var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/sort/index.tsx
var sort_exports = {};
__export(sort_exports, {
  default: () => sort_default
});
module.exports = __toCommonJS(sort_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_classnames = __toESM(require("classnames"));
var import_icons = require("@ant-design/icons");
var import_SwitchVertical01 = __toESM(require("@pisell/icon/es/SwitchVertical01"));
var import_platform = require("../../utils/platform");
var import_locales = require("../../locales");
var import_InlineMenu = __toESM(require("./components/InlineMenu"));
var import_index = require("./index.less");
var prefix = "pisell-lowcode-";
var Label = ({ content }) => {
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `${prefix}sort-menu-item-label` }, /* @__PURE__ */ import_react.default.createElement("span", null, content), /* @__PURE__ */ import_react.default.createElement(import_icons.CheckOutlined, null));
};
var sortChildrenMap = {
  date: (key, isLocal) => [
    {
      key: `${key}:asc${isLocal ? ":date" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-oldest-to-newest") })
    },
    {
      key: `${key}:desc${isLocal ? ":date" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-newest-to-oldest") })
    }
  ],
  time: (key, isLocal) => [
    {
      key: `${key}:desc${isLocal ? ":time" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-from-morning-to-night") })
    },
    {
      key: `${key}:asc${isLocal ? ":time" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-from-evening-to-morning") })
    }
  ],
  string: (key, isLocal) => [
    {
      key: `${key}:asc${isLocal ? ":string" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-a-z") })
    },
    {
      key: `${key}:desc${isLocal ? ":string" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-z-a") })
    }
  ],
  number: (key, isLocal) => [
    {
      key: `${key}:asc${isLocal ? ":number" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-0-9") })
    },
    {
      key: `${key}:desc${isLocal ? ":number" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-9-0") })
    }
  ],
  option: (key, isLocal) => [
    {
      key: `${key}:asc${isLocal ? ":option" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-first-last") })
    },
    {
      key: `${key}:desc${isLocal ? ":option" : ""}`,
      label: /* @__PURE__ */ import_react.default.createElement(Label, { content: (0, import_locales.getText)("sort-last-first") })
    }
  ]
};
var Sort = ({
  list = [],
  onChange,
  button,
  style,
  value,
  placement,
  localPagination
}) => {
  const sortItems = (0, import_react.useMemo)(() => {
    return list.map((d) => {
      const children = d.getSortItems ? d.getSortItems(d.name).map(({ key, label }) => ({ key, label: /* @__PURE__ */ import_react.default.createElement(Label, { content: label }) })) : sortChildrenMap[d.type](d.name, !!(d == null ? void 0 : d.localFilter));
      return {
        key: d.name,
        label: d.label,
        children,
        popupClassName: `${prefix}sort-sub-menu-popup-wrap ${prefix}dropdown-css-var`,
        className: `${d.isHidden ? `${prefix}sort-menu-wrap-hide` : ""}`
      };
    });
  }, [list, localPagination]);
  const onClick = (detail) => {
    var _a, _b;
    if (((_a = detail.keyPath) == null ? void 0 : _a[0]) === value) {
      onChange == null ? void 0 : onChange("", detail);
      return;
    }
    onChange == null ? void 0 : onChange((_b = detail == null ? void 0 : detail.keyPath) == null ? void 0 : _b[0], detail);
  };
  const isMobileBool = (0, import_platform.isMobile)();
  const btnContent = (0, import_react.useMemo)(() => {
    return button || /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        icon: /* @__PURE__ */ import_react.default.createElement(import_SwitchVertical01.default, null),
        style,
        className: (0, import_classnames.default)({
          [`${prefix}sort-button-active`]: !!value
        }),
        size: "large"
      },
      isMobileBool ? null : (0, import_locales.getText)("sort-button-text")
    );
  }, [button, style, value, isMobileBool]);
  const content = (0, import_react.useMemo)(() => {
    return /* @__PURE__ */ import_react.default.createElement(import_InlineMenu.default, { items: sortItems, value: value || "", onChange });
  }, [value, sortItems, onChange]);
  if ((0, import_platform.isMobile)()) {
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Popover,
      {
        placement,
        content,
        arrow: false,
        overlayClassName: `${prefix}mobile-sort-overlay`,
        trigger: ["click"]
      },
      btnContent
    );
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Dropdown,
    {
      trigger: ["click"],
      placement,
      menu: {
        items: sortItems,
        onClick,
        selectable: true,
        selectedKeys: value != null && value !== "" ? [value] : []
      }
    },
    btnContent
  );
};
var sort_default = Sort;
