import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { DialogConfigType } from "../../_setters/cDataSource-setter/type";
import { RequestService } from "../../../src/utils/RequestService";
import { LowCodeWebGidKey } from "../../../src/utils/type";
import { DataSourceType } from "../formKeyType";
import { SchemaService } from "../schemaService";
export var JsonDomSelectService = /*#__PURE__*/function () {
  function JsonDomSelectService() {}
  var _proto = JsonDomSelectService.prototype;
  _proto.showEditIcon = function showEditIcon(formKey, data) {
    var _data$;
    var result = false;
    if (formKey === DataSourceType.customStateType && (data === null || data === void 0 ? void 0 : (_data$ = data[0]) === null || _data$ === void 0 ? void 0 : _data$.length) > 0) {
      var key = LowCodeWebGidKey.dataSourceResponse + "_custom_" + data[0];
      var customData = sessionStorage.getItem(key);
      if (customData) {
        result = true;
      }
    } else {
      var _data$2;
      result = formKey === DataSourceType.dataSourceType && (data === null || data === void 0 ? void 0 : (_data$2 = data[0]) === null || _data$2 === void 0 ? void 0 : _data$2.length) > 0;
    }
    return result;
  }
  /**
   * 获取数据源或者自定义数据的渲染内容
   * @param formKey 
   * @param data 
   */;
  _proto.getJsonRenderData =
  /*#__PURE__*/
  function () {
    var _getJsonRenderData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(formKey, data) {
      var _data$3, _data$4, _data$5;
      var result, res, key, customData;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            result = {
              jsonSelectValue: '',
              renderJson: {}
            };
            if (data && data !== null && data !== void 0 && (_data$3 = data[1]) !== null && _data$3 !== void 0 && _data$3.length) {
              result.jsonSelectValue = this.getJsonValueByData(data[1]);
            }
            if (!(formKey === DataSourceType.dataSourceType && data !== null && data !== void 0 && (_data$4 = data[0]) !== null && _data$4 !== void 0 && _data$4.length)) {
              _context.next = 9;
              break;
            }
            _context.next = 5;
            return this.getDataSourceRenderJsonData(data[0]);
          case 5:
            res = _context.sent;
            if (res) {
              result.renderJson = res;
            }
            _context.next = 10;
            break;
          case 9:
            if (formKey === DataSourceType.customStateType && data !== null && data !== void 0 && (_data$5 = data[0]) !== null && _data$5 !== void 0 && _data$5.length) {
              key = LowCodeWebGidKey.dataSourceResponse + "_custom_" + data[0];
              customData = sessionStorage.getItem(key);
              if (customData) {
                result.renderJson.realData = JSON.parse(customData) || {};
              }
            }
          case 10:
            return _context.abrupt("return", result);
          case 11:
          case "end":
            return _context.stop();
        }
      }, _callee, this);
    }));
    function getJsonRenderData(_x, _x2) {
      return _getJsonRenderData.apply(this, arguments);
    }
    return getJsonRenderData;
  }()
  /**
   * 先尝试从服务端获取的数据，如果没有，再从本地数据中获取
   * @param dataSourceId 
   */
  ;
  _proto.getDataSourceRenderJsonData =
  /*#__PURE__*/
  function () {
    var _getDataSourceRenderJsonData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(dataSourceId) {
      var result, schemaService, dataList, webGid, dataSourceItem, requestService, res, temData;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            result = {};
            schemaService = new SchemaService();
            dataList = schemaService.getDataSourceList();
            webGid = schemaService.getWebGidBySchemaProps();
            dataSourceItem = dataList.find(function (item) {
              return item.id === dataSourceId;
            });
            if (!dataSourceItem) {
              _context2.next = 18;
              break;
            }
            requestService = new RequestService(); // 符合白名单的尝试走请求
            if (!this.isWhiteListRequest(dataSourceItem)) {
              _context2.next = 13;
              break;
            }
            requestService.loadWebGid(webGid);
            _context2.next = 11;
            return requestService.getTemRequestResponse(dataSourceItem.options.dataSource);
          case 11:
            res = _context2.sent;
            result.realData = (res === null || res === void 0 ? void 0 : res.code) === 200 ? this.getDataSourceJson(res) : this.getDataBySessionStorage(dataSourceId);
          case 13:
            if (!(dataSourceItem.options.dataSource.selectValue === DialogConfigType.ENGINE)) {
              _context2.next = 18;
              break;
            }
            _context2.next = 16;
            return requestService.getProjectFunctionByApiFunc(dataSourceItem);
          case 16:
            temData = _context2.sent;
            if (temData && (temData.responseTemplate || temData.responsePath)) {
              result.templateData = temData.responseTemplate;
              result.shortcutPath = temData.responsePath;
            }
          case 18:
            if (result.realData === undefined) {
              result.realData = this.getDataBySessionStorage(dataSourceId);
            }
            return _context2.abrupt("return", result);
          case 20:
          case "end":
            return _context2.stop();
        }
      }, _callee2, this);
    }));
    function getDataSourceRenderJsonData(_x3) {
      return _getDataSourceRenderJsonData.apply(this, arguments);
    }
    return getDataSourceRenderJsonData;
  }();
  _proto.getDataBySessionStorage = function getDataBySessionStorage(dataSourceId) {
    var result;
    // 否则都使用默认数据
    var key = LowCodeWebGidKey.dataSourceResponse + "_" + dataSourceId;
    var sourceData = sessionStorage.getItem(key);
    if (sourceData) {
      var sourceDataObj = JSON.parse(sourceData) || {};
      result = this.getDataSourceJson(sourceDataObj);
    }
    return result;
  }

  /**
   * 判断此数据源是否在白名单中
   * 目前仅支持，中心化接口，并且是引擎，并且是方法是getRule
   * @param dataSourceItem 
   */;
  _proto.isWhiteListRequest = function isWhiteListRequest(dataSourceItem) {
    var result = false;
    var dataSource = dataSourceItem.options.dataSource;
    var pbiFuncNameList = ['getRule'];
    if (dataSource.chainId === -1 && dataSource.selectValue === DialogConfigType.ENGINE) {
      var res = pbiFuncNameList.filter(function (fName) {
        return dataSource.pbiFuncName.includes(fName);
      });
      result = res.length > 0;
    }
    return result;
  }

  /**
   * 兼容不同的数据类型，最终的数据结构时不带code的
   * @param data 
   */;
  _proto.getDataSourceJson = function getDataSourceJson(res) {
    var result;
    if (res.code && res.data !== undefined) {
      result = this.getHandleRes(res);
    } else if (res.data !== undefined) {
      result = res.data;
      if (Object.prototype.toString.call(result) === '[object Object]') {
        result['_response'] = {
          code: 200,
          msg: '',
          status: true
        };
      }
    } else if (Object.prototype.toString.call(res) === '[object Object]') {
      result = res;
    }
    return result;
  };
  _proto.getHandleRes = function getHandleRes(res) {
    var result = res.data;
    if (Object.prototype.toString.call(result) === '[object Object]') {
      result._response = {
        code: res.code,
        msg: res.msg,
        status: res.status
      };
    } else if (result === null || result === undefined || result === '') {
      result = {
        data: res.data,
        _response: {
          code: res.code,
          msg: res.msg,
          status: res.status
        }
      };
    }
    return result;
  };
  _proto.getJsonValueByData = function getJsonValueByData(data) {
    var result = data;
    if (data.includes("?")) {
      // 先去除?.[0]
      result = data.replace("?.[", "");
      result = result.replace("?", "");
    }
    return result;
  };
  return JsonDomSelectService;
}();