import { immutable, circular, excludedInJSON } from '../decorators';
import { LEVEL_ENUM, Vertex, Logic, Process, ProcessComponent } from '..';
import { findUsageService } from '../../service/common';
// src/views/designer/asl/src/service/common
// src/views/designer/asl/src/types/process/ProcessInterface.ts
/**
 * 接口类
 */
export class ProcessInterface extends Vertex {
    /**
     * 概念类型
     */
    @immutable()
    public readonly level: LEVEL_ENUM = LEVEL_ENUM.interface;
    /**
     * Id
     */
    @immutable()
    public readonly id: string = undefined;
    /**
     * InterfaceKey
     */
    @immutable()
    public readonly key: string = undefined;
    /**
     * 名称
     */
    @immutable()
    public readonly name: string = undefined;
    /**
     * 协议
     */
    @immutable()
    public readonly protocol: string = undefined;
    /**
     * 地址
     */
    @immutable()
    public readonly host: string = undefined;
    /**
     * 端口
     */
    @immutable()
    public readonly port: string = undefined;
    /**
     * 路径
     */
    @immutable()
    public readonly path: string = undefined;
    /**
     * 方法
     */
    @immutable()
    public readonly method: string = undefined;
    /**
     * 描述
     */
    @immutable()
    public readonly description: string = undefined;
    /**
     * 逻辑 Id
     */
    @immutable()
    public readonly logicId: string = undefined;
    /**
     * 逻辑
     */
    @excludedInJSON()
    @immutable()
    public readonly logic: Logic = undefined;
    /**
     * Service 类型
     */
    @immutable()
    public readonly serviceType: 'micro' | 'entity' | 'process' = undefined;
    /**
     * Service Id
     */
    @immutable()
    public readonly serviceId: string = undefined;
    /**
     * Service 名称
     */
    @immutable()
    public readonly serviceName: string = undefined;
    /**
     * Process
     */
    @circular()
    @immutable()
    public readonly process: Process = undefined;
    /**
     * ProcessComponent
     */
    @circular()
    @immutable()
    public readonly processComponent: ProcessComponent = undefined;
    /**
     * 参数
     */
    @immutable()
    public readonly parameters: string = undefined;
    /**
     * 返回值
     */
    @immutable()
    public readonly responses: string = undefined;
    /**
     * 请求数据
     */
    @immutable()
    public readonly requestBody: string = undefined;
    /**
     * 树组件的子节点字段
     */
    @excludedInJSON()
    @immutable()
    public readonly moreChildrenFields: Array<string> = ['logic.params', 'logic.returns', 'logic.variables'];
    /**
     * 周边存在的名称
     */
    @excludedInJSON()
    public existingNames: Array<string> = [];
    /**
     * @param source 需要合并的部分参数
     */
    constructor(source?: Partial<ProcessInterface>) {
        super();
        source && this.assign(source);
    }
    /**
     * 按当前 id 加载接口数据
     * @requires this.id
     */
    load() {
        return this;
    }
    /**
     * 从后端 JSON 生成规范的 ProcessInterface 对象
     */
    public static from(source: any, parent: Process | ProcessComponent) {
        const item = new ProcessInterface(source);
        item.assign({
            key: `#/${parent.id}/${item.id}`,
            expanded: false,
        });
        item.logic && item.assign({ logic: Logic.from(item.logic, item) });
        if (parent instanceof Process) {
            item.assign({ process: parent });
        } else if (parent instanceof ProcessComponent) {
            item.assign({ processComponent: parent });
        }

        return item;
    }

    /**
     * 查找引用
     */
    async findUsage() {
        let id = this.id;
        if (['entity', 'micro', 'process', 'processComponent'].includes(this.serviceType)) {
            id = this.logicId;
        }
        const result = await findUsageService.findUsage({
            query: {
                id,
            },
        });

        return result;
    }
}

export default ProcessInterface;
