"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SimpleFilter = exports.AdvancedFilter = void 0;
var _react = _interopRequireWildcard(require("react"));
var _utils = require("@teamix/utils");
var _core = require("@formily/core");
var _ProForm = _interopRequireDefault(require("../ProForm"));
var _utils2 = require("../utils");
require("./index2.scss");
var _excluded = ["schema", "triggerType", "onFilter"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var getEventProps = function getEventProps(triggerType, onFilter) {
  var keydownEvent = {
    onSubmit: onFilter
  };
  var changeEvent = {
    onChange: onFilter
  };
  var eventMap = {
    keydown: keydownEvent,
    change: _objectSpread(_objectSpread({}, keydownEvent), changeEvent),
    submit: keydownEvent
  };
  return eventMap[triggerType];
};
/**
 * 简单筛选
 */
var SimpleFilter = /*#__PURE__*/(0, _react.memo)(function (props) {
  var schema = props.schema,
    triggerType = props.triggerType,
    onFilter = props.onFilter,
    otherProps = _objectWithoutProperties(props, _excluded);
  var prefixCls = (0, _utils.usePrefixCls)('teamix-pro-form-filter-simple');
  var simpleSchema = (0, _react.useMemo)(function () {
    return [{
      component: 'FormFlex',
      props: {
        size: 8
      },
      children: [].concat(_toConsumableArray(schema), [{
        type: 'void',
        decorator: null,
        component: 'Submit',
        props: {
          style: _objectSpread({}, triggerType === 'submit' ? {} : {
            display: 'none'
          })
        }
      }])
    }];
  }, [schema, triggerType]);
  var eventProps = getEventProps(triggerType, onFilter);
  return /*#__PURE__*/_react.default.createElement(_ProForm.default, _objectSpread(_objectSpread(_objectSpread({
    feedbackLayout: "popover",
    breakpoints: [],
    inset: true
  }, otherProps), eventProps), {}, {
    className: (0, _utils.cls)(prefixCls(), props.className),
    schema: simpleSchema
  }));
});
exports.SimpleFilter = SimpleFilter;
SimpleFilter.defaultProps = {
  triggerType: 'change'
};
/**
 * 高级筛选
 */
var AdvancedFilter = /*#__PURE__*/(0, _react.memo)(function (props) {
  var form = (0, _react.useMemo)(function () {
    return (props === null || props === void 0 ? void 0 : props.form) || (0, _core.createForm)();
  }, []);
  var schema = props.schema,
    triggerType = props.triggerType,
    onFilter = props.onFilter,
    onReset = props.onReset,
    layout = props.layout,
    labelAlign = props.labelAlign,
    wrapperAlign = props.wrapperAlign,
    labelCol = props.labelCol,
    wrapperCol = props.wrapperCol;
  var prefixCls = (0, _utils.usePrefixCls)('teamix-pro-form-filter-advanced');
  var gridSchema = (0, _react.useMemo)(function () {
    return [{
      name: 'FormGrid',
      component: 'FormGrid',
      props: {
        breakpoints: [480, 720, 990, 1200, Infinity],
        maxColumns: [1, 2, 3, 3, 4],
        columnGap: 16,
        rowGap: 12
      },
      children: [].concat(_toConsumableArray(schema), [{
        component: 'FormGrid.GridColumn',
        props: {
          gridSpan: -1
        },
        children: [{
          component: 'FormButtonGroup',
          props: {
            align: 'right'
          },
          children: [{
            component: 'Reset',
            props: {
              onResetValidateSuccess: onReset,
              style: _objectSpread({}, triggerType === 'submit' ? {} : {
                marginRight: -16
              }),
              children: (0, _utils.getMessage)('reset')
            }
          }, {
            component: 'Submit',
            props: {
              style: _objectSpread({}, triggerType === 'submit' ? {
                marginRight: -8
              } : {
                display: 'none'
              }),
              children: (0, _utils.getMessage)('search')
            }
          }]
        }]
      }])
    }];
  }, [schema, onReset, triggerType]);
  var getTeamixLayout = (0, _react.useMemo)(function () {
    return {
      breakpoints: [990],
      layout: (0, _utils2.mergeArrayValue)(['vertical'], layout),
      labelAlign: (0, _utils2.mergeArrayValue)(['left'], labelAlign),
      wrapperAlign: (0, _utils2.mergeArrayValue)(['left'], wrapperAlign),
      labelCol: (0, _utils2.mergeArrayValue)([24], labelCol),
      wrapperCol: (0, _utils2.mergeArrayValue)([24], wrapperCol)
    };
  }, [layout, labelAlign, wrapperAlign, labelCol, wrapperCol]);
  var eventProps = getEventProps(triggerType, onFilter);
  return /*#__PURE__*/_react.default.createElement(_ProForm.default, _objectSpread(_objectSpread(_objectSpread(_objectSpread({
    feedbackLayout: "popover"
  }, props), eventProps), getTeamixLayout), {}, {
    form: form,
    className: (0, _utils.cls)(prefixCls(), props.className),
    schema: gridSchema,
    onSubmit: onFilter
  }));
});
exports.AdvancedFilter = AdvancedFilter;
AdvancedFilter.defaultProps = {
  triggerType: 'change',
  layout: 'horizontal',
  labelAlign: 'right',
  labelCol: 6,
  wrapperCol: 18
};
var _default = AdvancedFilter;
exports.default = _default;