import { immutable, excludedInJSON, action } from '../decorators';
import { history, utils, LEVEL_ENUM, Logic, Param, Process } from '..';
import { convert2RefType } from '../data/dataTypeUtils';
import { getBasicTypeDefaultValue } from '../data/basicTypes';
import { schemaService } from '../../service/common';

/**
 * 流程输入参数
 */
export class ProcessProperty extends Param {
    /**
     * 概念类型
     */
    @immutable()
    public readonly level: LEVEL_ENUM = LEVEL_ENUM.processProperty;
    /**
     * 视图
     */
    @excludedInJSON()
    @immutable()
    public readonly process: Process = undefined;
    /**
     * 默认值
     * 按 JSON string 处理
     * - string: 666 -> '666'
     * - string: true -> 'true'
     * - number: 666 -> 666
     * - boolean: true -> true
     */
    public defaultValue: string = undefined;
    /**
     * @param source 需要合并的部分参数
     */
    constructor(source?: Partial<ProcessProperty>) {
        super();
        source && this.assign(source);
    }
    genCode() {
        return `ID_${this.process.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_param',
                },
            });
            return result;
        }
    }
    /**
     * 从后端 JSON 生成规范的 ProcessProperty 对象
     */
    public static from(source: any, process: Process | Logic) {
        convert2RefType(source.schema);
        source.process = process;
        source.code = `ID_${process.id}.ID_${source.id}`;
        const processProperty = new ProcessProperty(source);
        processProperty.genSchemaChildren();
        return processProperty;
    }
}

export default ProcessProperty;
