import { immutable, excludedInJSON, action } from '../decorators';
import { config, history, LEVEL_ENUM, Vertex, Logic, PackageJSON, App, WebService, Page, Block, Schema, genRefSchemaChildren } from '..';
import { vertexsMap } from '../cacheData';

/**
 * 开发时所有的变量，一般用于数据类型变更等
 */
export const variablesMap: Map<string, BaseVariable> = new Map();

export function updateVariablesChildrenSchema(schemaRef: string) {
    // Map 直接 forEach 会有问题
    const variables = Array.from(variablesMap, ([name, value]) => value);
    variables.forEach((variable) => {
        if (variable.schema && variable.schema.typeKey && variable.schema.typeKey === schemaRef) {
            variable.genSchemaChildren();
        }
    });
}
/**
 * 针对 param1.b.c 的形式，c 实体修改后需要改 param1 的 childrenSchema
 * 建立反向引用太复杂，干脆直接更新全量引用类型的变量结构
 */
export function updateAllVariablesChildrenSchema() {
    // Map 直接 forEach 会有问题
    const variables = Array.from(variablesMap, ([name, value]) => value);
    variables.forEach((variable) => {
        if (variable.schema && variable.schema.typeKey) {
            variable.genSchemaChildren();
        }
    });
}

/**
 * 逻辑输入参数
 */
export class BaseVariable extends Vertex {
    /**
     * 概念类型
     */
    @immutable()
    public readonly level: LEVEL_ENUM = LEVEL_ENUM.variable;
    /**
     * 参数类型
     * 这里暂时用不到，固定
     */
    @immutable()
    public readonly type = 'Identifier';
    /**
     * Id
     */
    @immutable()
    public readonly id: string = undefined;
    /**
     * 代码唯一标识
     */
    @immutable()
    public readonly code: string = undefined;
    /**
     * 名称
     */
    @immutable()
    public readonly name: string = undefined;
    /**
     * 描述
     */
    @immutable()
    public readonly description: string = undefined;
    /**
     * 位置
     */
    @immutable()
    public readonly _posIndex: number = undefined;
    /**
     * 逻辑Id
     */
    @immutable()
    public readonly logicId: string = undefined;
    /**
     * 逻辑
     */
    @excludedInJSON()
    @immutable()
    public readonly logic: Logic = undefined;
    /**
     * 描述
     */
    @immutable()
    public readonly schema: Schema = undefined;

    @excludedInJSON()
    @immutable()
    public readonly schemaChildren: Array<Schema> = undefined;
    /**
     * 是否必须
     */
    @immutable()
    public readonly required: boolean = undefined;
    /**
     * 节点是否为叶子节点
     * 前端 UI 状态
     */
    public isLeaf = true;
    /**
     * @param source 需要合并的部分参数
     */
    constructor(source?: Partial<BaseVariable>) {
        super();
        source && this.assign(source);
    }

    assign(source: any) {
        const oldId = this.id;
        oldId && variablesMap.delete(oldId);
        this.id && variablesMap.set(this.id, this);

        super.assign(source);
    }
    genCode() {
        return `ID_${this.id}`;
    }
    genSchemaChildren() {
        const children = !(this as any).entityFieldId ? genRefSchemaChildren(this.schema, this.code) : undefined;
        this.assign({
            schemaChildren: children,
            isLeaf: !children,
        });
    }
}

export default BaseVariable;
