import { getText } from "../../locales/index.js";
import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import usePisellConfig from "../pisell-config-provider/hooks/usePisellConfig.js";
import PisellText from "../pisellText/index.js";
import Amount from "../virtual-keyboard/Amount/index.js";
import { defaultConfig } from "./status.js";
import React, { useEffect, useMemo, useState } from "react";
import { setTheme } from "@pisell/utils";
import { Segmented } from "antd";
import classNames from "classnames";
import "./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 = _objectWithoutProperties(props, _excluded);
	const config = usePisellConfig();
	const [newValue, setNewValue] = useState(value || 0);
	const [updateMode, setUpdateMode] = useState("priceOverride");
	const [discountMode, setDiscountMode] = useState("amount");
	useEffect(() => {
		document.body.id = "body";
	}, []);
	/**
	* @title: 配置颜色
	* @description: 根据 props 和 selectType 生成最终的配置颜色
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-08-04 18:36
	*/
	const configColor = useMemo(() => {
		const currentConfig = defaultConfig[selectType];
		const validProps = Object.entries(props).reduce((acc, [key, value]) => {
			if (value !== void 0 && key in currentConfig) acc[key] = value;
			return acc;
		}, {});
		return _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 = useMemo(() => {
		if (discountMode === "amount") return originValue - Number(newValue || 0);
		return originValue - originValue * (Number(newValue || 0) / 100);
	}, [
		originValue,
		newValue,
		discountMode
	]);
	const isDiscountError = 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}%`;
	};
	useEffect(() => {
		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.createElement("div", { className: classNames("pisell-lowcode-adjust-price-wrap", {
		"pisell-lowcode-adjust-price-dark": selectType === "dark",
		"pisell-lowcode-adjust-price-light": selectType === "light"
	}) }, /* @__PURE__ */ React.createElement(Segmented, {
		size: "large",
		options: [{
			label: getText("pisell-adjust-price-price-override"),
			value: "priceOverride"
		}, {
			label: getText("pisell-adjust-price-discount"),
			value: "discount"
		}],
		value: updateMode,
		onChange: handleUpdateMode,
		block: true
	}), updateMode === "discount" && /* @__PURE__ */ React.createElement(Segmented, {
		size: "large",
		options: [{
			label: `${getText("pisell-adjust-price-discount-amount")}(${(config === null || config === void 0 ? void 0 : config.symbol) || "$"})`,
			value: "amount"
		}, {
			label: `${getText("pisell-adjust-price-discount-percentage")}(%)`,
			value: "percentage"
		}],
		value: discountMode,
		onChange: (val) => {
			setNewValue("");
			setDiscountMode(val);
		},
		block: true
	}), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode-adjust-price-amount-wrap" }, /* @__PURE__ */ React.createElement(Amount, _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.createElement("div", null, configColor.isShowMainText ? /* @__PURE__ */ React.createElement("div", { className: "pisell-low-code-adjust-price-total" }, isDiscountError ? /* @__PURE__ */ React.createElement(PisellText.Amount, { value: originValue }) : /* @__PURE__ */ React.createElement(React.Fragment, null, newValue && getText("pisell-adjust-price-total-after-discount"), /* @__PURE__ */ React.createElement(PisellText.Amount, { value: totalAfterDiscount }))) : null, configColor.isShowSubText ? /* @__PURE__ */ React.createElement("div", { className: classNames("pisell-low-code-adjust-price-amount-desc", { ["pisell-low-code-adjust-price-amount-desc-error"]: isDiscountError }) }, getText("pisell-adjust-price-discount-can-be-maximum"), " ", discountMode === "amount" ? /* @__PURE__ */ React.createElement(PisellText.Amount, {
		value: originValue,
		style: { color: configColor.subTextColor }
	}) : "100%") : null));
};
//#endregion
export { PisellAdjustPrice as default };
