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 _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, { useMemo } from 'react';
import { Button, Dropdown, Popover } from 'antd';
import classNames from 'classnames';
import { CheckOutlined } from '@ant-design/icons';
import SwitchVertical01 from '@pisell/icon/es/SwitchVertical01';
import { isMobile } from "../../utils/platform";
import { getText } from "../../locales";
import InlineMenu from "./components/InlineMenu";
import "./index.less";
var prefix = 'pisell-lowcode-';
var Label = function Label(_ref) {
  var content = _ref.content;
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "sort-menu-item-label")
  }, /*#__PURE__*/React.createElement("span", null, content), /*#__PURE__*/React.createElement(CheckOutlined, null));
};
var sortChildrenMap = {
  date: function date(key, isLocal) {
    return [{
      key: "".concat(key, ":asc").concat(isLocal ? ':date' : ''),
      label: /*#__PURE__*/React.createElement(Label, {
        content: getText('sort-oldest-to-newest')
      })
    }, {
      key: "".concat(key, ":desc").concat(isLocal ? ':date' : ''),
      label: /*#__PURE__*/React.createElement(Label, {
        content: getText('sort-newest-to-oldest')
      })
    }];
  },
  time: function time(key, isLocal) {
    return [{
      key: "".concat(key, ":desc").concat(isLocal ? ':time' : ''),
      label: /*#__PURE__*/React.createElement(Label, {
        content: getText('sort-from-morning-to-night')
      })
    }, {
      key: "".concat(key, ":asc").concat(isLocal ? ':time' : ''),
      label: /*#__PURE__*/React.createElement(Label, {
        content: getText('sort-from-evening-to-morning')
      })
    }];
  },
  string: function string(key, isLocal) {
    return [{
      key: "".concat(key, ":asc").concat(isLocal ? ':string' : ''),
      label: /*#__PURE__*/React.createElement(Label, {
        content: getText('sort-a-z')
      })
    }, {
      key: "".concat(key, ":desc").concat(isLocal ? ':string' : ''),
      label: /*#__PURE__*/React.createElement(Label, {
        content: getText('sort-z-a')
      })
    }];
  },
  number: function number(key, isLocal) {
    return [{
      key: "".concat(key, ":asc").concat(isLocal ? ':number' : ''),
      label: /*#__PURE__*/React.createElement(Label, {
        content: getText('sort-0-9')
      })
    }, {
      key: "".concat(key, ":desc").concat(isLocal ? ':number' : ''),
      label: /*#__PURE__*/React.createElement(Label, {
        content: getText('sort-9-0')
      })
    }];
  }
};
var Sort = function Sort(_ref2) {
  var _ref2$list = _ref2.list,
    list = _ref2$list === void 0 ? [] : _ref2$list,
    onChange = _ref2.onChange,
    button = _ref2.button,
    style = _ref2.style,
    value = _ref2.value,
    placement = _ref2.placement,
    localPagination = _ref2.localPagination;
  var sortItems = useMemo(function () {
    return list.map(function (d) {
      return {
        key: d.name,
        label: d.label,
        children: sortChildrenMap[d.type](d.name, !!(d !== null && d !== void 0 && d.localFilter)),
        popupClassName: "".concat(prefix, "sort-sub-menu-popup-wrap ").concat(prefix, "dropdown-css-var"),
        // 增加显示隐藏配置
        className: "".concat(d.isHidden ? "".concat(prefix, "sort-menu-wrap-hide") : '')
      };
    });
  }, [list, localPagination]);
  var onClick = function onClick(detail) {
    var _detail$keyPath, _detail$keyPath2;
    if (((_detail$keyPath = detail.keyPath) === null || _detail$keyPath === void 0 ? void 0 : _detail$keyPath[0]) === value) {
      onChange === null || onChange === void 0 ? void 0 : onChange('', detail);
      return;
    }
    onChange === null || onChange === void 0 ? void 0 : onChange(detail === null || detail === void 0 ? void 0 : (_detail$keyPath2 = detail.keyPath) === null || _detail$keyPath2 === void 0 ? void 0 : _detail$keyPath2[0], detail);
  };
  var isMobileBool = isMobile();
  var btnContent = useMemo(function () {
    return button || /*#__PURE__*/React.createElement(Button, {
      icon: /*#__PURE__*/React.createElement(SwitchVertical01, null),
      style: style,
      className: classNames(_defineProperty({}, "".concat(prefix, "sort-button-active"), !!value)),
      size: "large"
    }, isMobileBool ? null : getText('sort-button-text'));
  }, [button, style, value, isMobileBool]);
  var content = useMemo(function () {
    return /*#__PURE__*/React.createElement(InlineMenu, {
      items: sortItems,
      value: value || '',
      onChange: onChange
    });
  }, [value, sortItems, onChange]);
  if (isMobile()) {
    return /*#__PURE__*/React.createElement(Popover, {
      placement: placement,
      content: content,
      arrow: false,
      overlayClassName: "".concat(prefix, "mobile-sort-overlay"),
      trigger: ['click']
    }, btnContent);
  }
  return /*#__PURE__*/React.createElement(Dropdown, {
    trigger: ['click'],
    placement: placement,
    menu: {
      items: sortItems,
      onClick: onClick,
      selectable: true,
      selectedKeys: value
    }
  }, btnContent);
};
export default Sort;