"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.QuickFilterDataSet = exports.NewFilterDataSet = exports.ConditionDataSet = exports.AdvancedFieldSet = void 0;

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _omit = _interopRequireDefault(require("lodash/omit"));

var _DataSet = _interopRequireDefault(require("../../../data-set/DataSet"));

var _interface = require("../../../data-set/interface");

var _localeContext = require("../../../locale-context");

var _Operator = require("./Operator");

function processAxiosConfig() {
  var axiosConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var args = arguments.length > 1 ? arguments[1] : undefined;

  if (typeof axiosConfig === 'function') {
    return axiosConfig((0, _objectSpread2["default"])({}, args));
  }

  return axiosConfig;
}

function getTransportConfig(props) {
  var config = props.config,
      type = props.type,
      searchCode = props.searchCode,
      queryDataSet = props.queryDataSet,
      tableFilterAdapter = props.tableFilterAdapter;
  return processAxiosConfig(tableFilterAdapter, {
    type: type,
    config: config,
    searchCode: searchCode,
    queryDataSet: queryDataSet
  });
}

var ConditionDataSet = function ConditionDataSet() {
  return {
    paging: false,
    fields: [{
      name: 'fieldName',
      type: _interface.FieldType.string
    }, {
      name: 'comparator',
      type: _interface.FieldType.string,
      defaultValue: 'EQUAL'
    }, {
      name: 'conditionType',
      type: _interface.FieldType.string,
      defaultValue: 'regular'
    }, {
      name: 'value',
      transformRequest: function transformRequest(value) {
        if ((0, _typeof2["default"])(value) === 'object') {
          return JSON.stringify(value);
        }

        return value;
      },
      transformResponse: function transformResponse(value) {
        try {
          return JSON.parse(value);
        } catch (e) {
          return value;
        }
      }
    }],
    dataToJSON: _interface.DataToJSON.all,
    events: {
      update: function update() {}
    }
  };
};

exports.ConditionDataSet = ConditionDataSet;
var AdvancedFieldSet;
exports.AdvancedFieldSet = AdvancedFieldSet;

(function (AdvancedFieldSet) {
  AdvancedFieldSet["fieldName"] = "__fieldName__";
  AdvancedFieldSet["comparator"] = "__comparator__";
  AdvancedFieldSet["tableName"] = "__tableName__";
  AdvancedFieldSet["alias"] = "__alias__";
  AdvancedFieldSet["source"] = "__source__";
  AdvancedFieldSet["conditionType"] = "__conditionType__";
})(AdvancedFieldSet || (exports.AdvancedFieldSet = AdvancedFieldSet = {}));

var NewFilterDataSet = function NewFilterDataSet(_ref) {
  var propFields = _ref.propFields;
  var advancedFields = propFields.map(function (field) {
    return (0, _omit["default"])(field, ['defaultValue', 'ignore', 'cascadeMap']);
  });
  return {
    paging: false,
    selection: false,
    autoCreate: false,
    primaryKey: 'searchId',
    autoQuery: false,
    fields: [{
      name: AdvancedFieldSet.fieldName,
      type: _interface.FieldType.string,
      required: true
    }, {
      name: AdvancedFieldSet.comparator,
      type: _interface.FieldType.string,
      computedProps: {
        required: function required(_ref2) {
          var record = _ref2.record;
          return record.get(AdvancedFieldSet.fieldName);
        }
      },
      options: new _DataSet["default"]({
        selection: _interface.DataSetSelection.single,
        paging: false,
        data: _Operator.OPERATOR_TYPE.ALL
      })
    }, {
      name: AdvancedFieldSet.tableName,
      type: _interface.FieldType.string
    }, {
      name: AdvancedFieldSet.alias,
      type: _interface.FieldType.string
    }, {
      name: AdvancedFieldSet.source,
      type: _interface.FieldType.string
    }, {
      name: AdvancedFieldSet.conditionType,
      type: _interface.FieldType.string,
      defaultValue: 'comparator'
    }, {
      name: 'value',
      computedProps: {
        bind: function bind(_ref3) {
          var record = _ref3.record;
          return record.get(AdvancedFieldSet.fieldName);
        }
      },
      transformRequest: function transformRequest(value) {
        if ((0, _typeof2["default"])(value) === 'object') {
          return JSON.stringify(value);
        }

        return value;
      },
      transformResponse: function transformResponse(value) {
        try {
          return JSON.parse(value);
        } catch (e) {
          return value;
        }
      }
    }].concat((0, _toConsumableArray2["default"])(advancedFields)),
    dataToJSON: _interface.DataToJSON.dirty,
    events: {
      update: function update(_ref4) {
        var record = _ref4.record,
            name = _ref4.name,
            oldValue = _ref4.oldValue;
        var fieldName = record.get(AdvancedFieldSet.fieldName);
        var comparator = record.get(AdvancedFieldSet.comparator);

        if ([_Operator.OPERATOR.IS_NULL.value, _Operator.OPERATOR.IS_NOT_NULL.value].includes(comparator)) {
          record.set(fieldName, null);
        }

        if (name === AdvancedFieldSet.fieldName) {
          record.set(oldValue, null);

          if (comparator) {
            record.init(AdvancedFieldSet.comparator, null);
          }
        }
      }
    }
  };
};

exports.NewFilterDataSet = NewFilterDataSet;

var QuickFilterDataSet = function QuickFilterDataSet(_ref5) {
  var searchCode = _ref5.searchCode,
      queryDataSet = _ref5.queryDataSet,
      tableFilterAdapter = _ref5.tableFilterAdapter;
  return {
    paging: false,
    autoQuery: false,
    autoLocateFirst: false,
    primaryKey: 'searchId',
    transport: {
      adapter: function adapter(config, type) {
        return getTransportConfig({
          config: config,
          type: type,
          searchCode: searchCode,
          queryDataSet: queryDataSet,
          tableFilterAdapter: tableFilterAdapter
        });
      }
    },
    fields: [{
      name: 'searchName',
      label: (0, _localeContext.$l)('Table', 'filter_name'),
      type: 'string',
      maxLength: 20,
      required: true
    }, {
      name: 'searchId',
      type: 'string'
    }, // 条件逻辑值
    {
      name: 'conExpression',
      type: 'string',
      defaultValue: 'all',
      // 兼容旧数据
      transformResponse: function transformResponse(value) {
        if (!value) {
          return 'all';
        }

        return value;
      },
      transformRequest: function transformRequest(value) {
        return value || 'all';
      }
    }, {
      name: 'defaultFlag',
      type: 'boolean',
      falseValue: 0,
      trueValue: 1,
      label: (0, _localeContext.$l)('Table', 'set_default')
    }, {
      name: 'searchCode',
      type: 'string',
      defaultValue: searchCode
    }, {
      name: 'conditionList',
      type: 'object'
    }, {
      name: 'saveFilterValue',
      type: 'boolean',
      falseValue: 0,
      trueValue: 1,
      defaultValue: 1,
      // 默认保存筛选值，兼容旧数据
      transformResponse: function transformResponse(value) {
        if (value === 0) {
          return 0;
        }

        return 1;
      },
      label: (0, _localeContext.$l)('Table', 'save_filter_value')
    }, {
      // 是否租户默认配置
      name: 'isTenant',
      type: 'boolean',
      falseValue: 0,
      trueValue: 1
    }]
  };
};

exports.QuickFilterDataSet = QuickFilterDataSet;
//# sourceMappingURL=QuickFilterDataSet.js.map
