const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useSharedState = require("../../../hooks/useSharedState.js");
const require_model = require("../model.js");
const require_index = require("../../Pagination/index.js");
const require_useTransDataSource = require("../hooks/useTransDataSource.js");
const require_index$1 = require("./components/VirtualGrid/index.js");
const require_index$2 = require("./components/GalleryItem/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
require("./index.less");
//#region src/components/table/Gallery/index.tsx
const prefix = "pisell-lowcode-";
const 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 } = require_useSharedState.default(require_model.Context);
	const form = antd.Form.useFormInstance();
	const column_setting = antd.Form.useWatch("column_setting", {
		form,
		preserve: true
	}) || [];
	const gallery_setting = antd.Form.useWatch(["gallery_setting"], {
		form,
		preserve: true
	}) || {};
	const { originDataSource, originColumns } = state;
	const dataSource = require_useTransDataSource.default({
		dataSource: originDataSource,
		form,
		localPagination: !!(pagination === null || pagination === void 0 ? void 0 : pagination.localPagination),
		rowKey,
		filter,
		currentView: "gallery"
	});
	(0, react.useEffect)(() => {
		onDataSourceChange === null || onDataSourceChange === void 0 || onDataSourceChange(dataSource);
	}, [JSON.stringify(dataSource)]);
	const columnsMap = (0, react.useMemo)(() => {
		return new Map(originColumns.map((item) => [item.key, item]));
	}, [originColumns]);
	const renderItem = (0, react.useCallback)((record, index) => {
		return /* @__PURE__ */ react.default.createElement(require_index$2.default, {
			onClick: () => onItem === null || onItem === void 0 ? void 0 : onItem(record, index),
			columns: column_setting,
			gallerySetting: gallery_setting,
			record,
			gallery,
			columnsMap,
			index
		});
	}, [
		gallery_setting,
		gallery,
		columnsMap,
		column_setting
	]);
	const total = (0, react.useMemo)(() => {
		return pagination === null || pagination === void 0 ? void 0 : pagination.total;
	}, [pagination]);
	const gap = (0, react.useMemo)(() => {
		return [horizontalGutter, verticalGutter];
	}, [horizontalGutter, verticalGutter]);
	const contentIsShowLabel = (0, react.useMemo)(() => {
		if (typeof gallery.content.showLabel === "boolean") return gallery.content.showLabel;
		return true;
	}, [gallery.content.showLabel]);
	const rowHeight = (0, react.useMemo)(() => {
		var _header$fields;
		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 || gallery_setting === void 0 ? void 0 : gallery_setting.coverField) !== "noCover") coverHeight = cover.height;
		if (title.field) titleHeight = 32 * title.line + 36;
		if (header === null || header === void 0 || (_header$fields = header.fields) === null || _header$fields === void 0 ? void 0 : _header$fields.show) headerFieldHeight = 81 * header.fields.list.length;
		if (column_setting) {
			const fieldCount = column_setting.filter((item) => item.isShow).length;
			let itemHeight = 55;
			if (contentIsShowLabel && gallery_setting.isShowLabel && gallery_setting.compactMode !== "compact") itemHeight = 81;
			contentHeight = itemHeight * fieldCount;
			if (gallery_setting.compactMode === "compact") contentHeight = contentHeight * .8;
		}
		if ((0, _pisell_utils.isFunction)(footer === null || footer === void 0 ? void 0 : footer.render)) footerHeight = footer.height;
		return coverHeight + titleHeight + headerFieldHeight + contentHeight + footerHeight;
	}, [
		cardHeight,
		fixedHeight,
		cover,
		title,
		gallery_setting,
		column_setting,
		header,
		footer,
		contentIsShowLabel
	]);
	return /* @__PURE__ */ react.default.createElement(antd.Spin, {
		spinning: loading,
		wrapperClassName: `${prefix}gallery-spin`
	}, /* @__PURE__ */ react.default.createElement("div", { className: `${prefix}gallery-wrap` }, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		paddingTop: 4,
		itemMinWidth: cardMinWidth,
		itemMaxWidth: cardMaxWidth,
		dataSource,
		renderItem,
		gap,
		rowHeight
	}), !pagination.localPagination && /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "pagination",
		style: {
			margin: 0,
			position: "absolute",
			bottom: 0,
			left: 0,
			right: 0
		}
	}, /* @__PURE__ */ react.default.createElement(require_index.FormPagination, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, pagination), {}, { total })))));
};
var Gallery_default = (0, react.memo)(Gallery);
//#endregion
exports.default = Gallery_default;
