import mitt from 'mitt';
import { reactive } from 'vue';
import { SkinManager } from './skin';
import { NetlistRender } from './render';


export const emitter = mitt();

export const globalSetting = reactive({
    /**
     * @description 当前的系统语言
     * @type { 'zh' | 'en' | 'zhTw' | 'ja' | 'de' | 'ko' | 'ru' | 'fr' | 'ar' }
     */
    language: 'zh',

    /**
     * @description 开启渲染动画
     * @type {boolean}
     */
    renderAnimation: true,

    /**
     * @description 绘制箭头
     * @type {boolean}
     */
    renderArrow: false,

    /**
     * @description 加粗多位宽的线
     * @type {boolean}
     */
    boldMultiWidthWire: true,
    
    /**
     * @description 当前交叉点的样式
     * @type {'connect' | 'slice' | 'concat'} currentStyle
     */
    crossDotStyle: 'connect'
});

export const globalLookup = reactive({
    /**
     * @type {SkinManager}
     */
    skinManager: new SkinManager(),

    /**
     * @type {NetlistRender}
     */
    netlistRender: new NetlistRender(),

    /**
     * @type {Avoid}
     */
    Avoid: undefined,

    /**
     * @description 当前仿真结果的顶层模块的名字
     * @type {string}
     */
    topModuleName: '',

    /**
     * @description 当前选择的实体，可以是 wire，也可以是 cell
     */
    currentSelectEntity: undefined,

    /**
     * @description 右侧 treeview 选择的需要展示数据的实体
     * @type {Record<string, string>}
     */
    currentSelectEntityInfo: {}
});

function loadSetting() {
    const settingString = localStorage.getItem('setting')
    try {
        const setting = JSON.parse(settingString);
        return setting;
    } catch (error) {
        return undefined;
    }
}

export const storedSetting = loadSetting();
if (storedSetting) {
    for (const key of Reflect.ownKeys(storedSetting)) {
        const value = storedSetting[key];
        if (value !== undefined) {
            globalSetting[key] = value;
        }
    }
}

