import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useSharedState from "../../../hooks/useSharedState.js";
import { Context } from "../model.js";
import { FormPagination } from "../../Pagination/index.js";
import useTransDataSource from "../hooks/useTransDataSource.js";
import VirtualGrid from "./components/VirtualGrid/index.js";
import GalleryItem from "./components/GalleryItem/index.js";
import React, { memo, useCallback, useEffect, useMemo } from "react";
import { isFunction } from "@pisell/utils";
import { Form, Spin } from "antd";
import "./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 } = useSharedState(Context);
	const form = Form.useFormInstance();
	const column_setting = Form.useWatch("column_setting", {
		form,
		preserve: true
	}) || [];
	const gallery_setting = Form.useWatch(["gallery_setting"], {
		form,
		preserve: true
	}) || {};
	const { originDataSource, originColumns } = state;
	const dataSource = useTransDataSource({
		dataSource: originDataSource,
		form,
		localPagination: !!(pagination === null || pagination === void 0 ? void 0 : pagination.localPagination),
		rowKey,
		filter,
		currentView: "gallery"
	});
	useEffect(() => {
		onDataSourceChange === null || onDataSourceChange === void 0 || onDataSourceChange(dataSource);
	}, [JSON.stringify(dataSource)]);
	const columnsMap = useMemo(() => {
		return new Map(originColumns.map((item) => [item.key, item]));
	}, [originColumns]);
	const renderItem = useCallback((record, index) => {
		return /* @__PURE__ */ React.createElement(GalleryItem, {
			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 = useMemo(() => {
		return pagination === null || pagination === void 0 ? void 0 : pagination.total;
	}, [pagination]);
	const gap = useMemo(() => {
		return [horizontalGutter, verticalGutter];
	}, [horizontalGutter, verticalGutter]);
	const contentIsShowLabel = useMemo(() => {
		if (typeof gallery.content.showLabel === "boolean") return gallery.content.showLabel;
		return true;
	}, [gallery.content.showLabel]);
	const rowHeight = 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 (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.createElement(Spin, {
		spinning: loading,
		wrapperClassName: `${prefix}gallery-spin`
	}, /* @__PURE__ */ React.createElement("div", { className: `${prefix}gallery-wrap` }, /* @__PURE__ */ React.createElement(VirtualGrid, {
		paddingTop: 4,
		itemMinWidth: cardMinWidth,
		itemMaxWidth: cardMaxWidth,
		dataSource,
		renderItem,
		gap,
		rowHeight
	}), !pagination.localPagination && /* @__PURE__ */ React.createElement(Form.Item, {
		name: "pagination",
		style: {
			margin: 0,
			position: "absolute",
			bottom: 0,
			left: 0,
			right: 0
		}
	}, /* @__PURE__ */ React.createElement(FormPagination, _objectSpread2(_objectSpread2({}, pagination), {}, { total })))));
};
var Gallery_default = memo(Gallery);
//#endregion
export { Gallery_default as default };
