/**
 * 合并require @birt/数据源，模板/路径... 配置
 * @author cls
 * @date 2021-05-16
 * import { MergeRequire } from "@birt/funclib/MergeRequire";
 */
import { $CONST } from "@birt/funclib/ConstUtil";

export const MergeRequire = {
  // 判断是否本地相对src路径
  _isLocalUrl(url) {
    return url && url.startsWith("@");
  },
  // 获取引用对象
  getRequireObj(url) {
    if (url) {
      if (this._isLocalUrl(url)) {
        if (url.startsWith("@birt/datasources/")) {
          return this.getRequireDataset(...arguments);
        } else if (url.startsWith("@birt/templates/lookup/")) {
          return this.getRequireTemplateLookup(...arguments);
        } else if (url.startsWith("@birt/lookupmodel/")) {
          return this.getRequireLookModel(...arguments);
        }
      } else {
        // 处理服务端加载配置，待实现
      }
    } else {
      console.error("require url 引用路径未配置.")
    }
  },
  /**
   * 获取引用数据集对象
   * 
   * @param {*} url 数据模型路径
   */
  getRequireDataset(url) { // 判断是否本地引用
    url = url.replace($CONST.DATASOURCEROOT, "");
    return require("@birt/datasources/" + url).newInstance();
  },
  /**
   * 获取引用模板对象
   *
   * @param {*} url  引用对象路径 
   * @param {*} dataset 
   * @param {*} conf 
   */
  getRequireTemplateLookup(url, dataset, conf) {
    url = url.replace($CONST.LOOKUPROOT, "");
    return require("@birt/templates/lookup/" + url).newInstance(
      dataset,
      conf
    );
  },
  // 获取查找模型配置, 根据模型配置，定义查找页面内容
  getRequireLookModel(url, dataset, conf) {
    url = url.replace($CONST.LOOKMODELROOT, "");
    return require("@birt/lookupmodel/" + url).newInstance(conf);
  },
  // 查找报表模板路径
  getBirtUrl(lookup) {
    return lookup[$CONST.BIRTURL];
  },
  // 目标查找数据集配置
  getLookDataset(lookup) {
    return lookup.lookDataset || {};
  },
  // 获取目标查找数据集实例
  getLookDatasetInsc(lookup) {
    let lkDataset = this.getLookDataset(lookup);
    if (lkDataset) {
      let url = lkDataset.instanceUrl;
      if (!url) {
        console.error("查找定义目标数据集 lookDataset 未配置.");
      }
      let dsObj = this.getRequireObj(url);
      if (lkDataset.modelFilePath) {
        dsObj.modelFilePath = lkDataset.modelFilePath
      }
      return dsObj;
    }
  },
  // 获取查找模型路径
  getLookModelUrl(lookup) {
    return lookup[$CONST.LOOKUPMODEL];
  },
  // 获取关联字段列表，alias别名字段优先应用
  getJoinFields(sourceFields, joinFields) {
    let retuFields = {};
    let jFields = joinFields.split(",");
    jFields.forEach(f => {
      let sField = sourceFields[f];
      if (sField) {
        let alias = sField.alias || f;
        retuFields[alias] = sField;
      }
    });
    return JSON.parse(JSON.stringify(retuFields));
  },
  /**
   * 合并数据集字段引用
   * 
   * @param {*} fields 数据字段对象
   */
  mergeStoreFieldsRequire(fields) {
    debugger
    for (let fn in fields) {
      let fObj = fields[fn];
      let lookup = fObj.lookup;
      if (lookup) {
        let lkType = lookup.type;
        if (lkType == $CONST.LOOKUPSYSENUMS) continue;
        // 获取查找目标数据集实例
        let lkDatasetInsc = this.getLookDatasetInsc(lookup);
        if (lkDatasetInsc) {
          lookup.lookDataset.insc = lkDatasetInsc;
          // 获取查找模型引用 lookmodel
          let lkModelUrl = this.getLookModelUrl(lookup);
          let lkModel;
          if (lkModelUrl) {
            lkModel = this.getRequireObj(lkModelUrl, lkDatasetInsc, lookup);
            lookup[$CONST.LOOKUPMODELINSC] = lkModel;
          }
          // 获取查找页面引用 url
          let url = this.getBirtUrl(lookup);
          let lkPage = this.getRequireObj(url, lkDatasetInsc, lkModel || lookup);
          lookup[$CONST.BIRTMODEL] = lkPage;
        }
      }
    }
  },
  // 根据字段列表获取子字段集合
  getSubFields(bizFieds, fieldids) {
    let fields = fieldids.split(",");
    let subFields = {};
    fields.forEach(f => {
      subFields[f] = JSON.parse(JSON.stringify(bizFieds[f]));
    })
    return subFields;
  },
  /**
   * 合并数据集字段
   */
  getMergeFields(...fields) {
    let mergeFieds = {};
    fields.forEach(f => {
      Object.assign(mergeFieds, f);
    })
    this.mergeStoreFieldsRequire(mergeFieds);
    return mergeFieds;
  }
};