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/table/hooks/useTransColumns.tsx
var useTransColumns_exports = {};
__export(useTransColumns_exports, {
  default: () => useTransColumns_default
});
module.exports = __toCommonJS(useTransColumns_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_numberFilter = __toESM(require("../TableFilter/numberFilter"));
var import_stringFilter = __toESM(require("../TableFilter/stringFilter"));
var import_selectFilter = __toESM(require("../TableFilter/selectFilter"));
var import_SortIcon = __toESM(require("../TableFilter/SortIcon"));
var import_utils2 = require("../Table/fields/utils");
var import_locales = require("../../../locales");
var genOnCell = ({ bordered, position }) => (_) => {
  let styleObj = {};
  let notGroupStyle = {};
  const borderStr = bordered ? "1px solid #EAECF0" : "";
  if (position === "start") {
    styleObj = {
      borderLeft: borderStr,
      borderRadius: "8px 0px 0px 0px",
      padding: "12px"
    };
    notGroupStyle = {
      borderLeft: borderStr
    };
  } else if (position === "end") {
    styleObj = {
      borderRight: borderStr,
      borderRadius: "0px 8px 0px 0px"
    };
    notGroupStyle = {
      borderRight: borderStr
    };
  }
  return {
    style: _.__is_group ? {
      borderTop: "1px solid var(--Gray-200, #EAECF0)",
      ...styleObj
    } : {
      ...notGroupStyle
    }
  };
};
var useTransColumns = (params) => {
  var _a;
  const { columns, form, handleSave, handleResize, dragSort, bordered } = params;
  const group = import_antd.Form.useWatch("group_by", { form, preserve: true });
  const groupField = (_a = group == null ? void 0 : group[0]) == null ? void 0 : _a.field;
  const groupFieldRender = (0, import_react.useCallback)(
    (text, record) => {
      if (!groupField)
        return void 0;
      const { children } = record;
      const fieldCol = columns.find(
        (item) => `${item.dataIndex}` === groupField
      );
      const { render, dataIndex } = fieldCol || {};
      let dom = /* @__PURE__ */ import_react.default.createElement("div", null, (0, import_utils2.getValueByKeys)(children == null ? void 0 : children[0], dataIndex || groupField.split(",")));
      if (render) {
        dom = render(
          (0, import_utils2.getValueByKeys)(children == null ? void 0 : children[0], dataIndex),
          record.children[0]
        );
      }
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          style: {
            display: "flex",
            justifyContent: "space-between",
            alignItems: "start"
          }
        },
        /* @__PURE__ */ import_react.default.createElement("span", null, dom),
        /* @__PURE__ */ import_react.default.createElement("span", null, children.length, " ", (0, import_locales.getText)("table-column-group-records"))
      );
    },
    [groupField, columns]
  );
  const _columns = (0, import_react.useMemo)(() => {
    var _a2;
    let newColumns = columns.filter(
      (item) => !((0, import_utils.isBoolean)(item.isShow) && !item.isShow)
    );
    if (dragSort && !groupField && ((_a2 = newColumns == null ? void 0 : newColumns[0]) == null ? void 0 : _a2.key) !== "__sort") {
      newColumns.unshift({
        key: "__sort",
        width: 60,
        align: "center",
        className: "drag-sort-cell"
      });
    }
    return newColumns.map((col, index) => {
      var _a3;
      let position = "";
      if (index === 0) {
        position = "start";
      } else if (index === newColumns.length - 1) {
        position = "end";
      }
      let groupObj = {};
      if (groupField) {
        groupObj = {
          render: (text, record) => {
            var _a4;
            if (record.__is_group) {
              return null;
            }
            return ((_a4 = col == null ? void 0 : col.render) == null ? void 0 : _a4.call(col, text, record)) || text;
          }
        };
      }
      if (position === "start" && groupField) {
        groupObj = {
          fixed: "left",
          width: 350,
          render: (text, record) => {
            var _a4;
            if (record.__is_group && groupFieldRender) {
              return groupFieldRender(text, record);
            }
            return ((_a4 = col == null ? void 0 : col.render) == null ? void 0 : _a4.call(col, text, record)) || text;
          }
        };
      }
      const item = {
        editable: col.editable || true,
        dataIndex: col.dataIndex,
        title: col.title,
        field_type: col.field_type,
        lock: col.lock,
        width: col.width || 250,
        action: col.action || "",
        render: col.render,
        contentClassName: col.contentClassName || ""
      };
      let filterObj = {};
      if (col.defaultFilters) {
        if (col.field_type === "number") {
          filterObj = (0, import_numberFilter.default)({ dataIndex: col.dataIndex });
        }
        if (col.field_type === "text") {
          filterObj = (0, import_stringFilter.default)({ dataIndex: col.dataIndex });
        }
      }
      if (((_a3 = col.filters) == null ? void 0 : _a3.length) > 0) {
        filterObj = {
          ...(0, import_selectFilter.default)({
            dataIndex: col.dataIndex,
            filters: col.filters
          }),
          filters: col.filters
        };
      }
      return {
        ...col,
        ...filterObj,
        ...groupObj,
        sortIcon: (props) => /* @__PURE__ */ import_react.default.createElement(import_SortIcon.default, { ...props }),
        onCell: (record) => ({
          record,
          ...item,
          ...genOnCell({ position, bordered })(record),
          handleSave
        }),
        onHeaderCell: (record) => ({
          record,
          ...item,
          onResize: handleResize == null ? void 0 : handleResize(index),
          position
        })
      };
    });
  }, [columns, groupFieldRender, dragSort, groupField, bordered]);
  return {
    columns: _columns
  };
};
var useTransColumns_default = useTransColumns;
