import { Vertex, Entity } from './index';

export const vertexsMap: Map<string, Vertex> = new Map();
export const entitiesMap: Map<string, Entity> = new Map();

export function genFinalCode(code: string, finalCode = true) {
    if(!finalCode)
        return code;

    if(typeof code !== 'string')
        return code;
    // ID_ENUMVALUE_只需要返回ENUM的item的value值
    if(code.includes('ID_ENUMVALUE_')){
        code = code.replace(/ID_([\w-]+).ID_ENUMVALUE_([\w-]+)/g, (m) => {
            const id = m.split('.').pop().replace('ID_ENUMVALUE_', '');
            const param = vertexsMap.get(id);
            return  param && `'${(param as any).value}'` || `'${id}'`;
        });
    }
    if(code.includes('ID_ENUM_LIST_')){
        code = code.replace(/ID_ENUM_LIST_([\w-]+)/g, (m, id) => {
            const param = vertexsMap.get(id);
            return `$utils.EnumList('${param && param.name}')`;
        });
    }
    // @TODO: 最好后端把 ID 中的 - 都去掉
    return code.replace(/ID_([\w-]+)/g, (m, id) => {
        const param = vertexsMap.get(id);
        return (param as any)?.name
            || (param as any)?.asName 
            || id;
    });
}