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/Gallery/index.tsx
var Gallery_exports = {};
__export(Gallery_exports, {
  default: () => Gallery_default
});
module.exports = __toCommonJS(Gallery_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_Pagination = require("../../Pagination");
var import_useTransDataSource = __toESM(require("../hooks/useTransDataSource"));
var import_hooks = require("../../../hooks");
var import_model = require("../model");
var import_VirtualGrid = __toESM(require("./components/VirtualGrid"));
var import_GalleryItem = __toESM(require("./components/GalleryItem"));
var import_constant = require("./constant");
var import_index = require("./index.less");
var prefix = "pisell-lowcode-";
var Gallery = (props) => {
  const { gallery, filter, pagination, rowKey, loading, onDataSourceChange } = props;
  const {
    horizontalGutter,
    verticalGutter,
    cardMinWidth,
    cardMaxWidth,
    cardHeight,
    fixedHeight,
    cover,
    title,
    header,
    footer,
    onItem
  } = gallery || {};
  const { state } = (0, import_hooks.useSharedState)(import_model.Context);
  const form = import_antd.Form.useFormInstance();
  const column_setting = import_antd.Form.useWatch("column_setting", { form, preserve: true }) || [];
  const gallery_setting = import_antd.Form.useWatch(["gallery_setting"], {
    form,
    preserve: true
  }) || {};
  const { originDataSource, originColumns } = state;
  const dataSource = (0, import_useTransDataSource.default)({
    dataSource: originDataSource,
    form,
    localPagination: !!(pagination == null ? void 0 : pagination.localPagination),
    rowKey,
    filter
  });
  (0, import_react.useEffect)(() => {
    onDataSourceChange == null ? void 0 : onDataSourceChange(dataSource);
  }, [JSON.stringify(dataSource)]);
  const columnsMap = (0, import_react.useMemo)(() => {
    return new Map(originColumns.map((item) => [item.key, item]));
  }, [originColumns]);
  const renderItem = (0, import_react.useCallback)(
    (record, index) => {
      return /* @__PURE__ */ import_react.default.createElement(
        import_GalleryItem.default,
        {
          onClick: () => onItem == null ? void 0 : onItem(record, index),
          columns: column_setting,
          gallerySetting: gallery_setting,
          record,
          gallery,
          columnsMap,
          index
        }
      );
    },
    [gallery_setting, gallery, columnsMap, column_setting]
  );
  const total = (0, import_react.useMemo)(() => {
    return pagination == null ? void 0 : pagination.total;
  }, [pagination]);
  const gap = (0, import_react.useMemo)(() => {
    return [horizontalGutter, verticalGutter];
  }, [horizontalGutter, verticalGutter]);
  const rowHeight = (0, import_react.useMemo)(() => {
    var _a;
    if (fixedHeight) {
      return cardHeight;
    }
    let coverHeight = 0;
    let titleHeight = 0;
    let headerFieldHeight = 0;
    let contentHeight = 0;
    let footerHeight = 0;
    if (cover.show && (gallery_setting == null ? void 0 : gallery_setting.coverField) !== "noCover") {
      coverHeight = cover.height;
    }
    if (title.field) {
      titleHeight = import_constant.TITLE_LENE_HEIGHT * title.line + import_constant.TITLE_VERTICAL_SPACING * 2;
    }
    if ((_a = header == null ? void 0 : header.fields) == null ? void 0 : _a.show) {
      headerFieldHeight = (import_constant.FIELD_LABEL_HEIGHT + import_constant.FIELD_VALUE_HEIGHT) * header.fields.list.length;
    }
    if (column_setting) {
      const fieldCount = column_setting.filter(
        (item) => item.isShow
      ).length;
      const itemHeight = gallery_setting.isShowLabel && gallery_setting.compactMode !== "compact" ? import_constant.FIELD_LABEL_HEIGHT + import_constant.FIELD_VALUE_HEIGHT : import_constant.FIELD_VALUE_HEIGHT;
      contentHeight = itemHeight * fieldCount;
      if (gallery_setting.compactMode === "compact") {
        contentHeight = contentHeight * 0.8;
      }
    }
    if ((0, import_utils.isFunction)(footer == null ? void 0 : footer.render)) {
      footerHeight = footer.height;
    }
    return coverHeight + titleHeight + headerFieldHeight + contentHeight + footerHeight;
  }, [
    cardHeight,
    fixedHeight,
    cover,
    title,
    gallery_setting,
    column_setting,
    header,
    footer
  ]);
  return /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { spinning: loading, wrapperClassName: `${prefix}gallery-spin` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${prefix}gallery-wrap` }, /* @__PURE__ */ import_react.default.createElement(
    import_VirtualGrid.default,
    {
      paddingTop: 4,
      itemMinWidth: cardMinWidth,
      itemMaxWidth: cardMaxWidth,
      dataSource,
      renderItem,
      gap,
      rowHeight
    }
  ), !pagination.localPagination && /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: "pagination",
      style: {
        margin: 0,
        position: "absolute",
        bottom: 0,
        left: 0,
        right: 0
      }
    },
    /* @__PURE__ */ import_react.default.createElement(import_Pagination.FormPagination, { ...pagination, total })
  )));
};
var Gallery_default = (0, import_react.memo)(Gallery);
