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/index.tsx
var table_exports = {};
__export(table_exports, {
  default: () => table_default
});
module.exports = __toCommonJS(table_exports);
var import_ahooks = require("ahooks");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_classNames = __toESM(require("classNames"));
var import_Tabs = __toESM(require("./Tabs"));
var import_hooks = require("../../hooks");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_Actions = __toESM(require("./Actions"));
var import_Header = __toESM(require("./Header"));
var import_Summary = __toESM(require("./Summary"));
var import_utils = require("./Table/utils");
var import_View = __toESM(require("./View"));
var import_useTableSetting = __toESM(require("./hooks/useTableSetting"));
var import_useTransDataSourceGroupSetting = __toESM(require("./hooks/useTransDataSourceGroupSetting"));
var import_useTransFilterSetting = __toESM(require("./hooks/useTransFilterSetting"));
var import_useTransSortSetting = __toESM(require("./hooks/useTransSortSetting"));
var import_useTriggerValuesChange = __toESM(require("./hooks/useTriggerValuesChange"));
var import_model = require("./model");
var import_index = require("./index.less");
var defaultView = {
  defaultViewMode: "grid",
  multiple: false,
  modeList: []
};
var GridView = (0, import_model.Provider)(
  (0, import_react.forwardRef)((props, ref) => {
    var _a, _b, _c, _d, _e;
    const {
      dataSource,
      columns: propsColumns,
      dispatch,
      style,
      filter: propsFilter,
      sort: propsSort,
      buttons,
      actionButtons,
      title,
      showTitle = true,
      subTitle,
      showSubTitle = true,
      summary,
      onValuesChange,
      columnSetting,
      dataSourceGroup: propsDataSourceGroup,
      view = defaultView,
      gallery,
      currentSettingKey,
      // pagination,
      exportImport,
      currentComponentId,
      tabs,
      ...other
    } = props;
    const modal = (0, import_hooks.useSharedState)(import_model.Context);
    const [form] = import_antd.Form.useForm();
    const { defaultViewMode, multiple } = view;
    const viewMode = import_antd.Form.useWatch("view_mode", { form, preserve: true });
    const context = (0, import_useEngineContext.default)();
    const wrapRef = (0, import_react.useRef)(null);
    const tableSettingRef = (0, import_react.useRef)({});
    const filterDefaultRef = (0, import_react.useRef)({});
    const initialValuesRef = (0, import_react.useRef)({});
    const { width } = (0, import_ahooks.useSize)(wrapRef) || {};
    const debouncedWidth = (0, import_ahooks.useDebounce)(width, { wait: 100 });
    const utils = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils;
    const isTerminal = (0, import_react.useMemo)(() => {
      var _a2;
      return (_a2 = utils == null ? void 0 : utils.isTerminal) == null ? void 0 : _a2.call(utils);
    }, [utils]);
    const tableId = (0, import_react.useMemo)(() => {
      if (props.__designMode === "design") {
        return "designMode-mock-table-id";
      }
      return (props == null ? void 0 : props.currentComponentId) || props.__id;
    }, [props.__id, props.__designMode, props == null ? void 0 : props.currentComponentId]);
    const columns = (0, import_react.useMemo)(() => {
      return propsColumns.filter((item) => !item.hidden);
    }, [propsColumns]);
    const originColumnsMap = (0, import_react.useMemo)(
      () => new Map(
        columns.map((item) => [item.key, (0, import_model.formatColumn)(item)])
      ),
      [columns]
    );
    const sort = (0, import_useTransSortSetting.default)({
      sort: propsSort,
      originColumnsMap
    });
    const filter = (0, import_useTransFilterSetting.default)({
      filter: propsFilter,
      originColumnsMap
    });
    const dataSourceGroup = (0, import_useTransDataSourceGroupSetting.default)({
      dataSourceGroup: propsDataSourceGroup,
      originColumnsMap
    });
    const currentViewMode = (0, import_react.useMemo)(() => {
      const localCurrentMode = (0, import_utils.getCurrentViewModeFromLocalStorage)(tableId);
      if (multiple) {
        return viewMode || localCurrentMode;
      }
      return viewMode || defaultViewMode;
    }, [viewMode, defaultViewMode, multiple]);
    const removeTitleColumns = (0, import_react.useMemo)(() => {
      let _columns = columns.map((item) => {
        return {
          ...item,
          title: void 0
        };
      });
      return _columns;
    }, [columns]);
    const currentSettingHash = (0, import_react.useMemo)(() => {
      return currentSettingKey || (0, import_utils.getHash)(removeTitleColumns) || "";
    }, [currentSettingKey, JSON.stringify(removeTitleColumns)]);
    (0, import_react.useEffect)(() => {
      const setting = (0, import_utils.getTableSettingFromLocalStorage)(tableId);
      if (setting.currentSettingHash !== currentSettingHash) {
        (0, import_utils.clearTableSettingToLocalStorage)(tableId);
      }
    }, [currentSettingHash, tableId]);
    (0, import_react.useEffect)(() => {
      if (debouncedWidth) {
        modal.dispatch({
          type: "setTableWidth",
          payload: debouncedWidth || {}
        });
      }
    }, [debouncedWidth]);
    const { setTableSetting, tableSetting } = (0, import_useTableSetting.default)({
      tableId,
      filter,
      columns,
      columnSetting,
      dataSourceGroup,
      sort,
      view,
      currentViewMode,
      gallery,
      currentSettingHash
    });
    const getIsTriggerValuesChange = (0, import_useTriggerValuesChange.default)({
      filter,
      columnSetting,
      dataSourceGroup,
      sort,
      currentViewMode
    });
    (0, import_react.useEffect)(() => {
      modal.dispatch({
        type: "setUtils",
        payload: utils || {}
      });
      modal.dispatch({
        type: "setIsTerminal",
        payload: isTerminal
      });
    }, [utils, isTerminal]);
    (0, import_react.useEffect)(() => {
      modal.dispatch({
        type: "setDefaultColumns",
        payload: (0, import_utils.mergeColumnSetting)({ columns, tableId, currentViewMode })
      });
      modal.dispatch({
        type: "setOriginColumns",
        payload: columns
      });
    }, [columns, tableId, currentViewMode]);
    (0, import_react.useEffect)(() => {
      modal.dispatch({
        type: "setDataSource",
        payload: dataSource
      });
      modal.dispatch({
        type: "setOriginDataSource",
        payload: dataSource
      });
    }, [dataSource]);
    (0, import_react.useEffect)(() => {
      modal.dispatch({
        type: "setTableId",
        payload: tableId
      });
    }, [tableId]);
    (0, import_react.useEffect)(() => {
      var _a2;
      modal.dispatch({
        type: "setLocalPagination",
        payload: (_a2 = other == null ? void 0 : other.pagination) == null ? void 0 : _a2.localPagination
      });
    }, [(_b = other == null ? void 0 : other.pagination) == null ? void 0 : _b.localPagination]);
    (0, import_react.useEffect)(() => {
      setTimeout(() => {
        document.body.id = "body";
      }, 200);
    }, []);
    const filterDefaultValues = (0, import_react.useMemo)(() => {
      var _a2, _b2;
      const defaultValues = ((filter == null ? void 0 : filter.list) || []).reduce(
        (p, c) => {
          var _a3;
          return {
            ...p,
            [c.name]: (_a3 = c == null ? void 0 : c.other) == null ? void 0 : _a3.defaultValue
          };
        },
        {
          pagination: {
            page: ((_a2 = other.pagination) == null ? void 0 : _a2.current) || 1,
            size: ((_b2 = other.pagination) == null ? void 0 : _b2.pageSize) || 10
          }
        }
      );
      return defaultValues;
    }, [
      JSON.stringify(filter == null ? void 0 : filter.list),
      (_c = other.pagination) == null ? void 0 : _c.current,
      (_d = other.pagination) == null ? void 0 : _d.pageSize
    ]);
    filterDefaultRef.current = filterDefaultValues;
    tableSettingRef.current = tableSetting;
    initialValuesRef.current = {
      ...filterDefaultValues,
      ...tableSetting
    };
    (0, import_react.useImperativeHandle)(ref, () => {
      if (wrapRef.current) {
        wrapRef.current.formInstance = form;
      }
      return wrapRef.current;
    });
    (0, import_react.useEffect)(() => {
      form.setFieldsValue(initialValuesRef.current);
    }, [currentViewMode]);
    (0, import_react.useEffect)(() => {
      form.setFieldValue("column_setting", {});
      form.setFieldValue(
        "column_setting",
        tableSettingRef.current.column_setting
      );
    }, [(0, import_utils.stringify)(removeTitleColumns)]);
    (0, import_react.useEffect)(() => {
      form.setFieldsValue(initialValuesRef.current);
    }, [(0, import_utils.stringify)(propsFilter)]);
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form,
      {
        form,
        layout: "vertical",
        onValuesChange: (cur, values) => {
          var _a2;
          let _values = {
            ...form.getFieldsValue(true),
            ...values.filters || {}
          };
          let _cur = {
            ...cur,
            ...cur.filters || {}
          };
          const currentKey = Object.keys(cur)[0];
          if (currentKey !== "pagination") {
            _values.pagination = {
              ..._values.pagination,
              page: 1
            };
          } else {
            if ((_a2 = other == null ? void 0 : other.pagination) == null ? void 0 : _a2.localPagination) {
              return;
            }
          }
          setTableSetting(_values);
          form.setFieldsValue({
            pagination: _values.pagination
          });
          if (!getIsTriggerValuesChange(cur, values)) {
            return;
          }
          onValuesChange == null ? void 0 : onValuesChange((0, import_utils.omit)(_cur, ["filters"]), _values);
        },
        className: "materials-grid-form-wrap"
      },
      /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: (0, import_classNames.default)("materials-grid", {
            "materials-grid-terminal": isTerminal
          }),
          style,
          ref: wrapRef
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_Header.default,
          {
            title,
            showTitle,
            subTitle,
            showSubTitle,
            buttons
          }
        ),
        (tabs == null ? void 0 : tabs.show) && /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { name: "tabs", noStyle: true }, /* @__PURE__ */ import_react.default.createElement(import_Tabs.default, { ...tabs })),
        /* @__PURE__ */ import_react.default.createElement(
          import_Actions.default,
          {
            view,
            buttons: actionButtons,
            columnSetting,
            dataSourceGroup,
            filter,
            sort,
            tableId,
            localPagination: (_e = other == null ? void 0 : other.pagination) == null ? void 0 : _e.localPagination,
            gallery,
            exportImport
          }
        ),
        /* @__PURE__ */ import_react.default.createElement(import_Summary.default, { summary }),
        /* @__PURE__ */ import_react.default.createElement(
          import_View.default,
          {
            ...props,
            filter,
            sort,
            setTableSetting,
            dataSourceGroup
          }
        )
      )
    );
  })
);
var table_default = (0, import_react.forwardRef)(GridView);
