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); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import React, { useCallback, useMemo } from 'react';
import DotsSix from '@pisell/icon/es/DotsSix';
import HelpCircle from '@pisell/icon/es/HelpCircle';
import "./index.less";
import FilterItem from "../FilterItem";
import { Tooltip } from 'antd';
import MultipleContainersSortList from "../../../MultipleContainersSortList";
import { getText } from "../../../../locales";
var titleMap = {
  quickFilter: getText('table-action-filter-quick-filter'),
  otherFilter: getText('table-action-filter-other-filter')
};
var tooltipMap = {
  quickFilter: getText('table-action-filter-quick-filter-tip'),
  otherFilter: getText('table-action-filter-other-filter-tip')
};
var DragItem = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var attributes = props.attributes,
    listeners = props.listeners,
    setActivatorNodeRef = props.setActivatorNodeRef,
    style = props.style;
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: ref
  }, attributes, {
    style: style,
    className: "filter-dropdown-drag-item"
  }), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(DotsSix, _extends({
    ref: setActivatorNodeRef
  }, listeners, {
    style: _objectSpread(_objectSpread({}, listeners.style || {}), {}, {
      touchAction: 'none'
    }),
    className: "dots-six-icon"
  }))), /*#__PURE__*/React.createElement("div", {
    className: "filter-dropdown-drag-item-right"
  }, props.children));
});
var Dropdown = function Dropdown(props) {
  var quickFilter = props.quickFilter,
    otherFilter = props.otherFilter,
    onChange = props.onChange,
    quickFilterMaxLength = props.quickFilterMaxLength,
    formFiltersPrefix = props.formFiltersPrefix,
    _onOpen = props.onOpen,
    _onClose = props.onClose,
    renderItem = props.renderItem;
  var list = useMemo(function () {
    return {
      quickFilter: quickFilter || [],
      otherFilter: otherFilter || []
    };
  }, [quickFilter, otherFilter]);
  var handleSortChange = function handleSortChange(list) {
    onChange === null || onChange === void 0 ? void 0 : onChange(list);
  };
  var renderContainer = useCallback(function (_ref) {
    var children = _ref.children,
      ref = _ref.ref,
      id = _ref.id;
    return /*#__PURE__*/React.createElement("div", {
      className: "filter-dropdown-container-wrap"
    }, /*#__PURE__*/React.createElement("div", {
      className: "filter-dropdown-container-title-wrap"
    }, /*#__PURE__*/React.createElement("span", {
      className: "filter-dropdown-container-title"
    }, titleMap[id]), /*#__PURE__*/React.createElement(Tooltip, {
      title: tooltipMap[id](quickFilterMaxLength)
    }, /*#__PURE__*/React.createElement(HelpCircle, {
      className: "filter-dropdown-container-icon-help"
    }))), /*#__PURE__*/React.createElement("div", {
      ref: ref,
      className: "filter-dropdown-container-filter-wrap"
    }, children));
  }, [quickFilterMaxLength]);
  return /*#__PURE__*/React.createElement("div", {
    className: "filter-dropdown-wrap"
  }, /*#__PURE__*/React.createElement("div", {
    className: "filter-dropdown-wrap-content"
  }, /*#__PURE__*/React.createElement(MultipleContainersSortList, {
    list: list,
    rowKey: "name",
    renderContainer: renderContainer,
    onChange: handleSortChange,
    quickFilterMaxLength: quickFilterMaxLength
  }, function (_ref2) {
    var _item$name, _item$other3;
    var item = _ref2.item,
      index = _ref2.index,
      style = _ref2.style,
      props = _ref2.props,
      attributes = _ref2.attributes,
      setActivatorNodeRef = _ref2.setActivatorNodeRef,
      listeners = _ref2.listeners;
    var name = [formFiltersPrefix, (_item$name = item.name) === null || _item$name === void 0 ? void 0 : _item$name.toString()].filter(Boolean);
    return /*#__PURE__*/React.createElement(DragItem, _extends({}, props, {
      index: index,
      attributes: attributes,
      setActivatorNodeRef: setActivatorNodeRef,
      listeners: listeners,
      style: style
    }), renderItem ? renderItem(item, {
      onOpen: _onOpen,
      onClose: _onClose
    }) : /*#__PURE__*/React.createElement(FilterItem, {
      key: name.join('-'),
      name: name,
      label: item.label,
      type: item.type,
      other: _objectSpread(_objectSpread({}, item.other), {}, {
        onOpen: function onOpen() {
          var _item$other, _item$other$onOpen;
          (_item$other = item.other) === null || _item$other === void 0 ? void 0 : (_item$other$onOpen = _item$other.onOpen) === null || _item$other$onOpen === void 0 ? void 0 : _item$other$onOpen.call(_item$other);
          _onOpen === null || _onOpen === void 0 ? void 0 : _onOpen();
        },
        onClose: function onClose() {
          var _item$other2, _item$other2$onClose;
          (_item$other2 = item.other) === null || _item$other2 === void 0 ? void 0 : (_item$other2$onClose = _item$other2.onClose) === null || _item$other2$onClose === void 0 ? void 0 : _item$other2$onClose.call(_item$other2);
          _onClose === null || _onClose === void 0 ? void 0 : _onClose();
        },
        style: _objectSpread(_objectSpread({}, ((_item$other3 = item.other) === null || _item$other3 === void 0 ? void 0 : _item$other3.style) || {}), {}, {
          width: '100%'
        })
      }),
      props: item.props
    }));
  })));
};
export default Dropdown;