"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("antd/es/menu/style");

var _menu = _interopRequireDefault(require("antd/es/menu"));

var _react = _interopRequireDefault(require("react"));

var _lodashEs = require("lodash-es");

var _classnames = _interopRequireDefault(require("classnames"));

var _utils = require("@parallel-line/utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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; }

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 _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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var QuickSearch = function QuickSearch(props) {
  var prefixCls = "".concat(props.prefixCls, "-pro-table-search");
  var menus = props.menus,
      title = props.title,
      _props$menuParams = props.menuParams,
      menuParams = _props$menuParams === void 0 ? {} : _props$menuParams,
      statistics = props.statistics,
      menuBarRender = props.menuBarRender,
      _props$onMenuChange = props.onMenuChange,
      onMenuChange = _props$onMenuChange === void 0 ? function () {} : _props$onMenuChange;
  if (!menus && !props.toolBarRender) return null;

  var toolBar = /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-toolbar")
  }, props.toolBarRender);

  var renderTitle = function renderTitle() {
    return title ? /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(prefixCls, "-title")
    }, title) : null;
  };

  var renderMenu = function renderMenu(className, data) {
    if (typeof data === 'string') return renderTitle();

    var _Object$entries$ = _slicedToArray(Object.entries(data)[0], 2),
        name = _Object$entries$[0],
        value = _Object$entries$[1];

    var selectedKeys = menuParams[name];
    if (value === null || value === void 0 ? void 0 : value.title) return value === null || value === void 0 ? void 0 : value.title;
    var list = [];

    if (_utils.getDic && (value === null || value === void 0 ? void 0 : value.dicKey)) {
      list = _toConsumableArray((0, _utils.getDic)(value === null || value === void 0 ? void 0 : value.dicKey)).map(function (dic) {
        return _objectSpread(_objectSpread({}, dic), {}, {
          key: dic.key + ''
        });
      });
    } else if (value === null || value === void 0 ? void 0 : value.options) {
      list = value === null || value === void 0 ? void 0 : value.options.map(function (v) {
        // eslint-disable-next-line @typescript-eslint/no-shadow
        var _Object$entries$2 = _slicedToArray(Object.entries(v)[0], 2),
            key = _Object$entries$2[0],
            value = _Object$entries$2[1];

        return {
          key: key + '',
          value: value
        };
      });
    }

    if (list.length > 0 && !(value === null || value === void 0 ? void 0 : value.hideAll)) {
      if ((value === null || value === void 0 ? void 0 : value.allPosition) === 'right') {
        list.push({
          key: '__ALL__',
          value: '全部'
        });
      } else {
        list.unshift({
          key: '__ALL__',
          value: '全部'
        });
      }
    }

    return /*#__PURE__*/_react.default.createElement(_menu.default, {
      mode: "horizontal",
      className: className,
      selectedKeys: [selectedKeys || list[0].key],
      onClick: function onClick(e) {
        return onMenuChange(name, e.key);
      },
      style: {
        marginBottom: 1
      }
    }, list.map(function (v) {
      return /*#__PURE__*/_react.default.createElement(_menu.default.Item, {
        key: v.key
      }, v.value, (value === null || value === void 0 ? void 0 : value.statistics) ? "\uFF08".concat(statistics ? statistics[1][v.key] || 0 : 0, "\uFF09") : null);
    }));
  };

  var render = function render() {
    if (!menus || menus.length === 0) return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(prefixCls, "-menu-wrap")
    }, renderTitle(), toolBar), /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(prefixCls, "-filter-wrap")
    }));

    var _menus = _slicedToArray(menus, 2),
        m1 = _menus[0],
        m2 = _menus[1];

    var menuM = m1 ? (0, _lodashEs.omit)(m1, ['sign']) : title;
    var filterM = m2 ? (0, _lodashEs.omit)(m2, ['sign']) : null;
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _classnames.default)("".concat(prefixCls, "-menu-wrap"), !menuM && "".concat(prefixCls, "-menu-space"))
    }, menuM ? renderMenu("".concat(prefixCls, "-menu"), menuM) : filterM && renderMenu("".concat(prefixCls, "-filter"), filterM), toolBar), /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(prefixCls, "-filter-wrap")
    }, filterM && menuM && renderMenu("".concat(prefixCls, "-filter"), filterM)));
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    className: prefixCls
  }, render(), menuBarRender);
};

QuickSearch.defaultProps = {
  prefixCls: 'pxx'
};
var _default = QuickSearch;
exports.default = _default;