import { getText } from "../../locales/index.js";
import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import PisellCard from "../pisellCard/index.js";
import PisellModal from "../pisellModal/index.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import PisellText from "../pisellText/index.js";
import React, { useState } from "react";
import { Checkbox, Divider, Typography } from "antd";
import "./index.less";
//#region src/components/pisellGoodPassCard/index.tsx
const clsPrefix = "pisell-good-pass-card";
const index = (props) => {
	var _context$appHelper;
	const { translation } = (_context$appHelper = useEngineContext().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils;
	const { dataSource = [], onChange, style = {}, mixedSort } = props;
	const [showModal, setShowModal] = useState(false);
	console.log("pisell good card datasource", dataSource);
	const handleChange = (newValue) => {
		if (onChange) onChange(newValue);
	};
	const handleItemChange = (item, checked) => {
		handleChange({
			id: item.id,
			isSelected: checked
		});
	};
	const renderVoucherContent = (item) => {
		return /* @__PURE__ */ React.createElement("span", { className: `voucher-item-content-save` }, getText("pisell2.text.goodpass.save"), " ", /* @__PURE__ */ React.createElement(PisellText.Amount, { value: item.savedAmount }));
	};
	const renderActionElement = (item) => {
		const isChecked = item.isSelected;
		return /* @__PURE__ */ React.createElement(Checkbox, {
			disabled: item.isDisabled,
			style: { marginLeft: 8 },
			checked: isChecked,
			onChange: (e) => handleItemChange(item, e.target.checked)
		}, renderVoucherContent(item));
	};
	const renderVoucherItem = (item) => {
		return /* @__PURE__ */ React.createElement("div", {
			key: item.id,
			className: `${clsPrefix}-voucher-item`
		}, /* @__PURE__ */ React.createElement("div", { className: "item-title" }, translation(item.format_title), item.isEditMode && getText("pisell2.text.goodpass.last-settlement")), /* @__PURE__ */ React.createElement("div", { className: "item-content" }, renderActionElement(item)));
	};
	const renderModal = () => {
		return /* @__PURE__ */ React.createElement(PisellModal, {
			title: getText("pisell2.text.goodpass.product-vouchers"),
			footer: null,
			open: showModal,
			onCancel: () => setShowModal(false),
			className: `${clsPrefix}-all-items-modal`
		}, /* @__PURE__ */ React.createElement("div", { style: {
			padding: "10px 0",
			display: "flex",
			flexDirection: "column",
			gap: 12
		} }, dataSource.map(renderVoucherItem)));
	};
	const renderMixedMode = () => {
		const sortList = (mixedSort === null || mixedSort === void 0 ? void 0 : mixedSort(dataSource)) || dataSource;
		const list = sortList.length > 3 ? sortList.slice(0, 3) : sortList;
		return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: `${clsPrefix}-header` }, /* @__PURE__ */ React.createElement("div", null, getText("pisell2.text.goodpass.product-vouchers")), /* @__PURE__ */ React.createElement(Typography.Text, {
			underline: true,
			onClick: () => setShowModal(true)
		}, getText("pisell2.text.goodpass.view-more"))), /* @__PURE__ */ React.createElement(Divider, { style: { margin: 4 } }), /* @__PURE__ */ React.createElement("div", { className: `${clsPrefix}-body` }, list.map(renderVoucherItem)));
	};
	const renderModalMode = () => {
		const sortList = (mixedSort === null || mixedSort === void 0 ? void 0 : mixedSort(dataSource)) || dataSource;
		if (sortList.length === 0) return null;
		return /* @__PURE__ */ React.createElement("div", { style: {
			padding: "10px 0",
			display: "flex",
			flexDirection: "column",
			gap: 12
		} }, sortList.map(renderVoucherItem));
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, !props.isInModal ? renderModal() : null, dataSource.length > 0 ? /* @__PURE__ */ React.createElement(PisellCard, {
		padding: 8,
		borderRadius: 8,
		style: _objectSpread2({ width: 378 }, style),
		className: clsPrefix
	}, props.isInModal ? renderModalMode() : renderMixedMode()) : null);
};
//#endregion
export { index as default };
