import { immutable, excludedInJSON } from '../decorators';
import { LEVEL_ENUM, Logic, Param, ProcessComponent } from '..';
import { convert2RefType } from '../data/dataTypeUtils';
import { schemaService } from '../../service/common';

/**
 * 流程输入参数
 */
export class ProcessComponentProperty extends Param {
    /**
     * 概念类型
     */
    @immutable()
    public readonly level: LEVEL_ENUM = LEVEL_ENUM.processComponentProperty;
    /**
     * 视图
     */
    @excludedInJSON()
    @immutable()
    public readonly processComponent: ProcessComponent = undefined;
    /**
     * 默认值
     * 按 JSON string 处理
     * - string: 666 -> '666'
     * - string: true -> 'true'
     * - number: 666 -> 666
     * - boolean: true -> true
     */
    public defaultValue: string = undefined;
    /**
     * @param source 需要合并的部分参数
     */
    constructor(source?: Partial<ProcessComponentProperty>) {
        super();
        source && this.assign(source);
    }
    genCode() {
        return `ID_${this.processComponent.process.id}.ID_${this.processComponent.id}.ID_${this.id}`;
    }
    /**
     * 查找schema 顶点被引用的逻辑顶点列表
     */
    async getSchemaUsage() {
        if (this.schema) {
            const schemaId = this.schema.type === 'genericType' ? this.id : this.schema.id;
            const result = await schemaService.getSchemaUsage({
                query: {
                    schemaId,
                    valSource: 'process_component_property',
                },
            });
            return result;
        }
    }
    /**
     * 从后端 JSON 生成规范的 ProcessComponentProperty 对象
     */
    public static from(source: any, processComponent: ProcessComponent | Logic) {
        convert2RefType(source.schema);
        source.processComponent = processComponent;
        source.code = `ID_${(processComponent as ProcessComponent).process.id}.ID_${processComponent.id}.ID_${source.id}`;
        const processComponentProperty = new ProcessComponentProperty(source);
        processComponentProperty.genSchemaChildren();
        return processComponentProperty;
    }
}

export default ProcessComponentProperty;
