const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_usePisellConfig = require("../pisell-config-provider/hooks/usePisellConfig.js");
const require_index$1 = require("../pisellText/index.js");
const require_index$2 = require("../virtual-keyboard/Amount/index.js");
const require_status = require("./status.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/pisellAdjustPrice/index.tsx
const _excluded = [
	"value",
	"onChange",
	"defaultValue",
	"originValue",
	"selectType",
	"resetText",
	"doneText"
];
const PisellAdjustPrice = (props) => {
	const { value, onChange, defaultValue, originValue, selectType = "light", resetText, doneText } = props, others = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	const config = require_usePisellConfig.default();
	const [newValue, setNewValue] = (0, react.useState)(value || 0);
	const [updateMode, setUpdateMode] = (0, react.useState)("priceOverride");
	const [discountMode, setDiscountMode] = (0, react.useState)("amount");
	(0, react.useEffect)(() => {
		document.body.id = "body";
	}, []);
	/**
	* @title: 配置颜色
	* @description: 根据 props 和 selectType 生成最终的配置颜色
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-08-04 18:36
	*/
	const configColor = (0, react.useMemo)(() => {
		const currentConfig = require_status.defaultConfig[selectType];
		const validProps = Object.entries(props).reduce((acc, [key, value]) => {
			if (value !== void 0 && key in currentConfig) acc[key] = value;
			return acc;
		}, {});
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, currentConfig), validProps);
	}, [selectType, props]);
	const handleEnter = () => {
		if (isDiscountError) return;
		if (updateMode === "priceOverride") onChange === null || onChange === void 0 || onChange(Number(newValue));
		else onChange === null || onChange === void 0 || onChange(Number(totalAfterDiscount));
	};
	const handleChange = (val) => {
		setNewValue(val);
	};
	const totalAfterDiscount = (0, react.useMemo)(() => {
		if (discountMode === "amount") return originValue - Number(newValue || 0);
		return originValue - originValue * (Number(newValue || 0) / 100);
	}, [
		originValue,
		newValue,
		discountMode
	]);
	const isDiscountError = (0, react.useMemo)(() => {
		if (updateMode === "priceOverride") return false;
		return totalAfterDiscount < 0;
	}, [updateMode, totalAfterDiscount]);
	const handleUpdateMode = (val) => {
		setUpdateMode(val);
		setNewValue(val === "priceOverride" ? value || 0 : "");
	};
	const inputFormat = (v) => {
		if (updateMode === "priceOverride") return null;
		if (discountMode === "amount") return null;
		else return `${v}%`;
	};
	(0, react.useEffect)(() => {
		(0, _pisell_utils.setTheme)({
			"--pisell-adjust-price-text-color": configColor.textColor,
			"--pisell-adjust-price-background-color": configColor.backgroundColor,
			"--pisell-adjust-price-container-background-color": configColor.containerBackgroundColor,
			"--pisell-adjust-price-button-background-color": configColor.buttonBackgroundColor,
			"--pisell-adjust-price-main-text-color": configColor.mainTextColor,
			"--pisell-adjust-price-sub-text-color": configColor.subTextColor
		});
	}, [configColor]);
	return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("pisell-lowcode-adjust-price-wrap", {
		"pisell-lowcode-adjust-price-dark": selectType === "dark",
		"pisell-lowcode-adjust-price-light": selectType === "light"
	}) }, /* @__PURE__ */ react.default.createElement(antd.Segmented, {
		size: "large",
		options: [{
			label: require_index.getText("pisell-adjust-price-price-override"),
			value: "priceOverride"
		}, {
			label: require_index.getText("pisell-adjust-price-discount"),
			value: "discount"
		}],
		value: updateMode,
		onChange: handleUpdateMode,
		block: true
	}), updateMode === "discount" && /* @__PURE__ */ react.default.createElement(antd.Segmented, {
		size: "large",
		options: [{
			label: `${require_index.getText("pisell-adjust-price-discount-amount")}(${(config === null || config === void 0 ? void 0 : config.symbol) || "$"})`,
			value: "amount"
		}, {
			label: `${require_index.getText("pisell-adjust-price-discount-percentage")}(%)`,
			value: "percentage"
		}],
		value: discountMode,
		onChange: (val) => {
			setNewValue("");
			setDiscountMode(val);
		},
		block: true
	}), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode-adjust-price-amount-wrap" }, /* @__PURE__ */ react.default.createElement(require_index$2.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, others), {}, {
		selectType: selectType || "light",
		inputFormat,
		defaultValue: updateMode === "priceOverride" ? defaultValue : "",
		key: updateMode,
		value: newValue,
		defaultSelect: updateMode === "priceOverride",
		onChange: handleChange,
		onEnter: handleEnter,
		placeholder: discountMode === "amount" ? "0" : "%",
		resetText,
		doneText,
		rightItemsStyle: {
			buttonBackgroundColor: configColor.buttonBackgroundColor,
			textColor: configColor.textColor,
			resetButtonBackgroundColor: configColor.resetButtonBackgroundColor,
			resetButtonTextColor: configColor.resetButtonTextColor,
			isDoneButtonFollowTheme: configColor.isDoneButtonFollowTheme,
			doneButtonBackgroundColor: configColor.doneButtonBackgroundColor,
			doneButtonTextColor: configColor.doneButtonTextColor
		}
	}))), updateMode === "discount" && /* @__PURE__ */ react.default.createElement("div", null, configColor.isShowMainText ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-low-code-adjust-price-total" }, isDiscountError ? /* @__PURE__ */ react.default.createElement(require_index$1.default.Amount, { value: originValue }) : /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, newValue && require_index.getText("pisell-adjust-price-total-after-discount"), /* @__PURE__ */ react.default.createElement(require_index$1.default.Amount, { value: totalAfterDiscount }))) : null, configColor.isShowSubText ? /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("pisell-low-code-adjust-price-amount-desc", { ["pisell-low-code-adjust-price-amount-desc-error"]: isDiscountError }) }, require_index.getText("pisell-adjust-price-discount-can-be-maximum"), " ", discountMode === "amount" ? /* @__PURE__ */ react.default.createElement(require_index$1.default.Amount, {
		value: originValue,
		style: { color: configColor.subTextColor }
	}) : "100%") : null));
};
//#endregion
exports.default = PisellAdjustPrice;
