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/layouts/GridLayout/Grid.tsx
var Grid_exports = {};
__export(Grid_exports, {
  default: () => Grid_default
});
module.exports = __toCommonJS(Grid_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_useRecordBoardContext = require("../../hooks/useRecordBoardContext");
var import_recordBoardColumns = require("../../utils/recordBoardColumns");
var import_locales = require("../../../../locales");
var import_Grid = require("./Grid.less");
function getRowKey(record, rowKey) {
  if (typeof rowKey === "function") {
    return rowKey(record);
  }
  if (typeof rowKey === "string" && record != null && rowKey in record) {
    return record[rowKey];
  }
  return (record == null ? void 0 : record.id) ?? (record == null ? void 0 : record.key) ?? record;
}
var CONTEXT_ONLY_KEYS = [
  "data",
  "loading",
  "total",
  "pagination",
  "onPageChange",
  "searchParams",
  "onSearch",
  "onReset",
  "layoutType",
  "selectedKeys",
  "selectedRows",
  "onSelectionChange",
  "rowKey",
  "childComponentProps",
  "columnVisibility",
  "onColumnVisibilityChange",
  "columns",
  "scroll",
  "size",
  "defaultHiddenColumnKeys",
  "scrollAreaHeight",
  "setScrollAreaHeight",
  "collapseUniformColumns",
  "columnOrder",
  "onColumnOrderChange"
];
var RecordBoardGridView = (props) => {
  const ctx = (0, import_useRecordBoardContext.useRecordBoardContext)({ displayName: "grid" });
  const {
    render,
    columns: columnsProp,
    rowKey: rowKeyProp,
    rowSelection: rowSelectionProp,
    ...rest
  } = props;
  const rawColumns = columnsProp ?? ctx.columns ?? [];
  const columnVisibility = ctx.columnVisibility;
  const columnOrder = ctx.columnOrder;
  const collapseUniformColumns = ctx.collapseUniformColumns !== false;
  const summaryRef = (0, import_react.useRef)(null);
  const [uniformSummaryHeight, setUniformSummaryHeight] = (0, import_react.useState)(0);
  const {
    columns,
    uniformSummaryItems: summaryItems,
    processedColumnsForSummary
  } = (0, import_react.useMemo)(() => {
    let list = rawColumns;
    if (Array.isArray(columnOrder) && columnOrder.length > 0) {
      const keyToCol = new Map(
        list.map((c) => [(0, import_recordBoardColumns.getRecordBoardColumnKey)(c), c])
      );
      const ordered = [];
      for (const key of columnOrder) {
        const col = keyToCol.get(key);
        if (col) ordered.push(col);
      }
      list.forEach((col) => {
        const key = (0, import_recordBoardColumns.getRecordBoardColumnKey)(col);
        if (!columnOrder.includes(key)) ordered.push(col);
      });
      list = ordered;
    }
    const visibleByUser = columnVisibility == null ? list : list.filter(
      (col) => columnVisibility[(0, import_recordBoardColumns.getRecordBoardColumnKey)(col)] !== false
    );
    const data = ctx.data ?? [];
    const shouldCollapseUniform = collapseUniformColumns && data.length > 5;
    if (!shouldCollapseUniform) {
      return {
        columns: (0, import_recordBoardColumns.processColumnsForGrid)(visibleByUser),
        uniformSummaryItems: [],
        processedColumnsForSummary: void 0
      };
    }
    const processedAllVisible = (0, import_recordBoardColumns.processColumnsForGrid)(visibleByUser);
    const { uniformHiddenColumnKeys, summaryItems: items } = (0, import_recordBoardColumns.computeUniformColumnSummary)(data, visibleByUser);
    const finalSummaryItems = items.filter((item) => {
      const processedCol = processedAllVisible.find(
        (c) => (0, import_recordBoardColumns.getRecordBoardColumnKey)(c) === item.columnKey
      );
      const useRender = typeof (processedCol == null ? void 0 : processedCol.render) === "function";
      const content = useRender ? processedCol.render(item.cellValue, item.record ?? {}, 0) : item.type === "empty" ? (0, import_locales.getText)("record-board-uniform-summary-empty") : item.displayValue ?? (0, import_locales.getText)("record-board-uniform-summary-same");
      return !(0, import_recordBoardColumns.isTrivialUniformSummaryReactContent)(content);
    });
    const columnsForTable = processedAllVisible.filter(
      (col) => !uniformHiddenColumnKeys.includes((0, import_recordBoardColumns.getRecordBoardColumnKey)(col))
    );
    return {
      columns: columnsForTable,
      uniformSummaryItems: finalSummaryItems,
      processedColumnsForSummary: finalSummaryItems.length > 0 ? processedAllVisible : void 0
    };
  }, [
    rawColumns,
    columnVisibility,
    columnOrder,
    collapseUniformColumns,
    ctx.data
  ]);
  const tableRowKey = (0, import_react.useMemo)(() => {
    const key = ctx.rowKey ?? rowKeyProp ?? "id";
    return typeof key === "function" ? key : (record) => getRowKey(record, typeof key === "string" ? key : String(key));
  }, [ctx.rowKey, rowKeyProp]);
  const rowSelectionConfig = rowSelectionProp ?? ctx.rowSelection;
  const rowSelection = (0, import_react.useMemo)(() => {
    if (rowSelectionConfig === false)
      return void 0;
    const base = typeof rowSelectionConfig === "object" ? rowSelectionConfig : {};
    return {
      ...base,
      selectedRowKeys: ctx.selectedKeys,
      onChange: (selectedRowKeys, selectedRows) => {
        var _a;
        ctx.onSelectionChange(selectedRowKeys, selectedRows);
        (_a = base.onChange) == null ? void 0 : _a.call(base, selectedRowKeys, selectedRows);
      }
    };
  }, [ctx.selectedKeys, ctx.onSelectionChange, rowSelectionConfig]);
  (0, import_react.useEffect)(() => {
    if (summaryItems.length === 0) {
      setUniformSummaryHeight(0);
      return;
    }
    const el = summaryRef.current;
    if (!el) return;
    const ro = new ResizeObserver((entries) => {
      const entry = entries[0];
      if (entry) setUniformSummaryHeight(entry.contentRect.height);
    });
    ro.observe(el);
    return () => {
      ro.disconnect();
      setUniformSummaryHeight(0);
    };
  }, [summaryItems.length]);
  const gridTableProps = (0, import_react.useMemo)(() => {
    const picked = {};
    Object.keys(ctx).forEach((k) => {
      if (CONTEXT_ONLY_KEYS.includes(
        k
      ))
        return;
      picked[k] = ctx[k];
    });
    return picked;
  }, [ctx]);
  if (typeof render === "function") {
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, render({ ...ctx, ...rest }));
  }
  const dataSource = ctx.data ?? [];
  const isEmpty = !Array.isArray(dataSource) || dataSource.length === 0;
  const sumColumnWidths = (0, import_react.useMemo)(() => {
    return columns.reduce(
      (sum, col) => sum + (col.width ?? 250),
      0
    ) || 2e3;
  }, [columns]);
  const tableScroll = (0, import_react.useMemo)(() => {
    const scroll = rest.scroll ?? ctx.scroll;
    const scrollX = isEmpty && scroll && typeof scroll.x !== "number" ? sumColumnWidths : scroll == null ? void 0 : scroll.x;
    if ((scroll == null ? void 0 : scroll.autoCalc) && typeof ctx.scrollAreaHeight === "number") {
      const wrapHeight = ctx.scrollAreaHeight;
      const reservedHeight = 40 + uniformSummaryHeight + 24;
      return {
        x: scrollX ?? sumColumnWidths,
        y: Math.max(wrapHeight - reservedHeight, 100),
        scrollToFirstRowOnChange: scroll.scrollToFirstRowOnChange
      };
    }
    if (scroll && typeof scroll === "object") {
      const { autoCalc: _a, ...restScroll } = scroll;
      const resolved = Object.keys(restScroll).length ? restScroll : void 0;
      if (resolved) {
        return {
          ...resolved,
          x: scrollX ?? resolved.x ?? sumColumnWidths
        };
      }
      return resolved;
    }
    return scroll;
  }, [
    rest.scroll,
    ctx.scroll,
    ctx.scrollAreaHeight,
    ctx.pagination,
    columns,
    sumColumnWidths,
    isEmpty,
    uniformSummaryHeight
  ]);
  return /* @__PURE__ */ import_react.default.createElement("div", { "data-layout-slot": "grid", className: "pisell-lowcode-record-board-grid" }, summaryItems.length > 0 && /* @__PURE__ */ import_react.default.createElement("div", { ref: summaryRef, className: "pisell-record-board-uniform-summary" }, summaryItems.map((item) => {
    const processedCol = processedColumnsForSummary == null ? void 0 : processedColumnsForSummary.find(
      (c) => (0, import_recordBoardColumns.getRecordBoardColumnKey)(c) === item.columnKey
    );
    const useRender = typeof (processedCol == null ? void 0 : processedCol.render) === "function";
    const content = useRender ? processedCol.render(item.cellValue, item.record ?? {}, 0) : item.type === "empty" ? (0, import_locales.getText)("record-board-uniform-summary-empty") : item.displayValue ?? (0, import_locales.getText)("record-board-uniform-summary-same");
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: item.columnKey,
        className: "pisell-record-board-uniform-summary-item"
      },
      /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell-record-board-uniform-summary-label" }, item.title),
      /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell-record-board-uniform-summary-value" }, content)
    );
  })), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Table,
    {
      ...rest,
      ...gridTableProps,
      scroll: tableScroll,
      rowKey: tableRowKey,
      dataSource: ctx.data ?? [],
      loading: ctx.loading,
      columns,
      rowSelection,
      pagination: false
    }
  ));
};
var Grid_default = RecordBoardGridView;
