import { immutable } from '../../decorators';
import { ExpressionNode, LOGIC_TYPE } from '../LogicItem';
import { DestinationParam } from './DestinationParam';

/**
 * 跳转页面
 * @TODO: 当前类目前主要用于生成文档
 */
export class Destination extends ExpressionNode {
    /**
     * 逻辑节点类型
     */
    @immutable()
    public readonly type: LOGIC_TYPE = LOGIC_TYPE.Destination;
    /**
     * 页面路径
     */
    @immutable()
    public readonly page: string = undefined;
    /**
     * URL
     */
    @immutable()
    public readonly url: string = undefined;
    /**
     * Code
     */
    @immutable()
    public readonly code: string = undefined;
    /**
     * 跳转页面参数
     */
    @immutable()
    public readonly params: Array<DestinationParam> = [];
    /**
     * 生成 JS 脚本
     */
    toScript() {
        const params = this.params
            .filter((param) => param.pageParamKey && param.pageParamKeyValue)
            .map((param) => param.toScript());

        let result = this.code;
        if (params.length) {
            result = '`' + `${this.code}?${params.join('&')}` + '`';
        } else {
            result = '`' + `${result}` + '`';
        }

        return result.replace(/"/g, "'");
    }
}
