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/pisellRecordBoard/shellFrame/ToolBar/index.tsx
var ToolBar_exports = {};
__export(ToolBar_exports, {
  default: () => ToolBar_default
});
module.exports = __toCommonJS(ToolBar_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_SwitchVertical01 = __toESM(require("@pisell/icon/es/SwitchVertical01"));
var import_ToolBar = __toESM(require("../../../pisellGridPro/ToolBar"));
var import_pisellFind = require("../../../pisellFind");
var import_pisellFilter = __toESM(require("../../../pisellFilter"));
var import_sort = __toESM(require("../../../sort"));
var import_ToolBarReset = __toESM(require("../ToolBarReset"));
var import_ToolBarQuickFilter = __toESM(require("../ToolBarQuickFilter"));
var import_ColumnSetting = __toESM(require("../ColumnSetting"));
var import_ShellBodyMetaContext = require("../ShellBodyMetaContext");
var import_useRecordBoardContext = require("../../hooks/useRecordBoardContext");
var import_utils = require("@pisell/utils");
var import_locales = require("../../../../locales");
var import_ToolBarFilter = require("./ToolBarFilter.less");
var import_recordBoardToolbar = require("./recordBoardToolbar.less");
var import_recordBoardColumns = require("../../utils/recordBoardColumns");
function getSortButtonText() {
  const key = "pisell2.recordBoard.toolbar.sort";
  const u = import_utils.locales.getText(key);
  return u && u !== key ? u : (0, import_locales.getText)(key) || (0, import_locales.getText)("sort-button-text");
}
function getViewGridLabel() {
  const key = "pisell2.recordBoard.toolbar.viewGrid";
  const u = import_utils.locales.getText(key);
  return u && u !== key ? u : (0, import_locales.getText)(key) || "Table";
}
function getViewFloorMapLabel() {
  const key = "pisell2.recordBoard.toolbar.viewFloorMap";
  const u = import_utils.locales.getText(key);
  return u && u !== key ? u : (0, import_locales.getText)(key) || "Floor map";
}
var DEFAULT_SEARCH_FIELD = "keyword";
var FILTER_PARAMS_FIELD = "filter";
var SORT_PARAMS_FIELD = "sort";
function getDefaultFilterValue() {
  return {
    quickFilter: [],
    otherFilter: []
  };
}
var RecordBoardToolBar = (props) => {
  var _a, _b, _c;
  const ctx = (0, import_useRecordBoardContext.useRecordBoardContext)({ displayName: "toolBar" });
  const shellMeta = (0, import_ShellBodyMetaContext.useRecordBoardShellBodyMeta)();
  const { searchParams, onSearch, bodyView, setBodyView } = ctx;
  const hasGridInShell = (shellMeta == null ? void 0 : shellMeta.hasGridContent) ?? true;
  const hasFloorInShell = (shellMeta == null ? void 0 : shellMeta.hasFloorMapSlot) ?? ctx.floorMapProps != null;
  const floorForToggle = (shellMeta == null ? void 0 : shellMeta.effectiveFloorMapProps) ?? ctx.floorMapProps;
  const showTableFloorToggle = hasGridInShell && hasFloorInShell && floorForToggle != null;
  const slots = ((_a = ctx.childComponentProps) == null ? void 0 : _a.toolBar) ?? {};
  const keyword = (searchParams == null ? void 0 : searchParams[DEFAULT_SEARCH_FIELD]) ?? "";
  const handleSearch = (0, import_react.useCallback)(
    (value) => {
      onSearch({ ...searchParams, [DEFAULT_SEARCH_FIELD]: value || void 0 });
    },
    [onSearch, searchParams]
  );
  const gridColumns = (_c = (_b = ctx.childComponentProps) == null ? void 0 : _b.grid) == null ? void 0 : _c.columns;
  const derivedFilterFromColumns = (0, import_react.useMemo)(
    () => Array.isArray(gridColumns) && gridColumns.length > 0 ? (0, import_recordBoardColumns.deriveFilterFromColumns)(gridColumns) : null,
    [gridColumns]
  );
  const derivedSortFromColumnsList = (0, import_react.useMemo)(
    () => Array.isArray(gridColumns) && gridColumns.length > 0 ? (0, import_recordBoardColumns.deriveSortFromColumns)(gridColumns) : [],
    [gridColumns]
  );
  const searchConfig = slots.search;
  const hideSearch = searchConfig === false;
  const searchNode = hideSearch ? null : /* @__PURE__ */ import_react.default.createElement(
    import_pisellFind.PisellFind,
    {
      triggerType: "input",
      displayType: "inline",
      showConfirmButton: typeof searchConfig === "object" && searchConfig !== null && "showConfirmButton" in searchConfig ? searchConfig.showConfirmButton : true,
      value: keyword,
      onChange: (v) => handleSearch(v),
      onSearch: handleSearch,
      ...typeof searchConfig === "object" && searchConfig !== null ? searchConfig : {}
    }
  );
  const quickFilterConfig = slots.quickFilter;
  const quickFilterList = (0, import_react.useMemo)(() => {
    if (quickFilterConfig === false) return [];
    const fromConfig = typeof quickFilterConfig === "object" && quickFilterConfig !== null && Array.isArray(quickFilterConfig.filterList) ? quickFilterConfig.filterList : null;
    let list = fromConfig ?? (derivedFilterFromColumns == null ? void 0 : derivedFilterFromColumns.quickFilter) ?? [];
    const hideNames = typeof quickFilterConfig === "object" && quickFilterConfig !== null && Array.isArray(quickFilterConfig.hideFilterNamesOnFloorMap) ? quickFilterConfig.hideFilterNamesOnFloorMap : null;
    if (bodyView === "floorMap" && showTableFloorToggle && hideNames != null && hideNames.length > 0) {
      const hideSet = new Set(hideNames.map(String));
      list = list.filter((item) => {
        const n = item.name ?? item.key;
        return n == null || !hideSet.has(String(n));
      });
    }
    return list;
  }, [
    quickFilterConfig,
    derivedFilterFromColumns == null ? void 0 : derivedFilterFromColumns.quickFilter,
    bodyView,
    showTableFloorToggle
  ]);
  const showQuickFilterLeft = quickFilterList.length > 0;
  const quickFilterNode = showQuickFilterLeft ? /* @__PURE__ */ import_react.default.createElement(
    import_ToolBarQuickFilter.default,
    {
      filterList: quickFilterList,
      formFiltersPrefix: typeof quickFilterConfig === "object" && quickFilterConfig !== null ? quickFilterConfig.formFiltersPrefix ?? "" : "",
      size: typeof quickFilterConfig === "object" && quickFilterConfig !== null ? quickFilterConfig.size ?? "large" : "large"
    }
  ) : null;
  const bodyViewToggle = showTableFloorToggle ? /* @__PURE__ */ import_react.default.createElement(
    import_antd.Segmented,
    {
      className: "record-board-toolbar-body-view-segmented",
      size: "large",
      value: bodyView,
      onChange: (v) => setBodyView(v),
      options: [
        { label: getViewFloorMapLabel(), value: "floorMap" },
        { label: getViewGridLabel(), value: "grid" }
      ],
      style: { marginRight: 8 }
    }
  ) : null;
  const compactFloorMapToolbar = bodyView === "floorMap" && showTableFloorToggle;
  const hideBodyViewToggleInToolbarRow = showTableFloorToggle && Boolean(slots.tabs) && bodyView === "floorMap";
  const topLeft = compactFloorMapToolbar ? null : /* @__PURE__ */ import_react.default.createElement("div", { className: "record-board-toolbar-top-left" }, !hideBodyViewToggleInToolbarRow ? bodyViewToggle : null, quickFilterNode, searchNode ? /* @__PURE__ */ import_react.default.createElement("div", { className: "record-board-toolbar-find-wrap" }, searchNode) : null, slots.topLeft);
  const sortConfig = slots.sort;
  const hideSort = sortConfig === false;
  const sortValue = searchParams == null ? void 0 : searchParams[SORT_PARAMS_FIELD];
  const handleSortChange = (0, import_react.useCallback)(
    (val) => {
      onSearch({ ...searchParams, [SORT_PARAMS_FIELD]: val || void 0 });
    },
    [onSearch, searchParams]
  );
  const sortList = (0, import_react.useMemo)(() => {
    const fromConfig = typeof sortConfig === "object" && sortConfig !== null && Array.isArray(sortConfig.list) ? sortConfig.list : null;
    return fromConfig ?? derivedSortFromColumnsList;
  }, [sortConfig, derivedSortFromColumnsList]);
  const sortNode = hideSort ? null : /* @__PURE__ */ import_react.default.createElement(
    import_sort.default,
    {
      ...typeof sortConfig === "object" && sortConfig !== null ? sortConfig : {},
      list: sortList,
      value: sortValue,
      onChange: handleSortChange,
      placement: "bottomLeft",
      button: /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          className: "record-board-toolbar-filter-btn",
          icon: /* @__PURE__ */ import_react.default.createElement(import_SwitchVertical01.default, null),
          size: "large"
        },
        getSortButtonText()
      )
    }
  );
  const filterConfig = slots.filter;
  const hideFilter = filterConfig === false;
  const filterValue = searchParams == null ? void 0 : searchParams[FILTER_PARAMS_FIELD];
  const hasFilterList = Array.isArray(filterValue == null ? void 0 : filterValue.quickFilter) && filterValue.quickFilter.length > 0 || Array.isArray(filterValue == null ? void 0 : filterValue.otherFilter) && filterValue.otherFilter.length > 0;
  const hasFilterValues = typeof (filterValue == null ? void 0 : filterValue.values) === "object" && filterValue.values !== null && Object.keys(filterValue.values).length > 0;
  const hasFilterItems = hasFilterList || hasFilterValues;
  const defaultFilterFromConfig = typeof filterConfig === "object" && filterConfig !== null && filterConfig.defaultFilterValue ? filterConfig.defaultFilterValue : null;
  const effectiveFilterValue = (0, import_react.useMemo)(() => {
    const base = filterValue != null && hasFilterItems ? filterValue : defaultFilterFromConfig ? defaultFilterFromConfig : derivedFilterFromColumns && (derivedFilterFromColumns.quickFilter.length > 0 || derivedFilterFromColumns.otherFilter.length > 0) ? derivedFilterFromColumns : getDefaultFilterValue();
    const quickFilter = Array.isArray(base.quickFilter) && base.quickFilter.length > 0 ? base.quickFilter : (defaultFilterFromConfig == null ? void 0 : defaultFilterFromConfig.quickFilter) ?? [];
    let otherFilter = Array.isArray(base.otherFilter) && base.otherFilter.length > 0 ? base.otherFilter : (defaultFilterFromConfig == null ? void 0 : defaultFilterFromConfig.otherFilter) ?? [];
    if (otherFilter === base.otherFilter && Array.isArray(defaultFilterFromConfig == null ? void 0 : defaultFilterFromConfig.otherFilter) && defaultFilterFromConfig.otherFilter.length > 0) {
      const configMap = new Map(
        defaultFilterFromConfig.otherFilter.map((c) => [
          c.key ?? c.name ?? "",
          c
        ])
      );
      otherFilter = otherFilter.map((item) => {
        var _a2, _b2;
        const key = item.key ?? item.name ?? "";
        const configItem = configMap.get(key);
        if (!configItem) return item;
        return {
          ...configItem,
          ...item,
          label: configItem.label ?? item.label,
          other: {
            ...configItem == null ? void 0 : configItem.other,
            ...item == null ? void 0 : item.other,
            placeholder: ((_a2 = configItem == null ? void 0 : configItem.other) == null ? void 0 : _a2.placeholder) ?? ((_b2 = item == null ? void 0 : item.other) == null ? void 0 : _b2.placeholder)
          }
        };
      });
    }
    return {
      ...base,
      quickFilter,
      otherFilter,
      values: base.values ?? {}
    };
  }, [
    filterValue,
    hasFilterItems,
    defaultFilterFromConfig,
    derivedFilterFromColumns
  ]);
  const filterValueForPisellFilter = (0, import_react.useMemo)(
    () => ({
      ...effectiveFilterValue,
      quickFilter: []
    }),
    [effectiveFilterValue]
  );
  const handleFilterChange = (0, import_react.useCallback)(
    (value) => {
      const currentFilter = searchParams == null ? void 0 : searchParams[FILTER_PARAMS_FIELD];
      const currentValues = (currentFilter == null ? void 0 : currentFilter.values) ?? {};
      const incomingValues = (value == null ? void 0 : value.values) ?? {};
      const nextFilter = {
        ...value,
        values: { ...currentValues, ...incomingValues }
      };
      onSearch({ ...searchParams, [FILTER_PARAMS_FIELD]: nextFilter });
    },
    [onSearch, searchParams]
  );
  const filterResetKey = ctx.filterResetKey ?? 0;
  const filterNode = hideFilter ? null : /* @__PURE__ */ import_react.default.createElement(
    import_pisellFilter.default,
    {
      key: `record-board-filter-${filterResetKey}`,
      value: filterValueForPisellFilter,
      onChange: handleFilterChange,
      hasForm: true,
      quickFilterMaxLength: 100,
      formFiltersPrefix: "",
      size: "large",
      buttonClassName: "record-board-toolbar-filter-btn",
      hiddenQuickFilter: true,
      placement: "bottomLeft",
      ...typeof filterConfig === "object" && filterConfig !== null ? filterConfig : {}
    }
  );
  const showColumnSetting = slots.columnSetting !== false && Array.isArray(gridColumns) && gridColumns.length > 0;
  const topRight = compactFloorMapToolbar ? null : /* @__PURE__ */ import_react.default.createElement(import_antd.Space, null, showColumnSetting ? /* @__PURE__ */ import_react.default.createElement(import_ColumnSetting.default, null) : null, sortNode, filterNode, /* @__PURE__ */ import_react.default.createElement(import_ToolBarReset.default, { size: "large" }), slots.topRight);
  return /* @__PURE__ */ import_react.default.createElement(
    import_ToolBar.default,
    {
      className: (0, import_classnames.default)(
        bodyView === "floorMap" && "pisell-record-board-toolbar--floor-map",
        bodyView === "grid" && "pisell-record-board-toolbar--grid",
        slots.className
      ),
      style: slots.style,
      tabs: slots.tabs,
      topLeft,
      topRight,
      bottomLeft: compactFloorMapToolbar ? null : slots.bottomLeft,
      bottomRight: compactFloorMapToolbar ? null : slots.bottomRight
    }
  );
};
RecordBoardToolBar.displayName = "RecordBoard.ToolBar";
var ToolBar_default = RecordBoardToolBar;
