var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellAdjustPrice/index.tsx
var pisellAdjustPrice_exports = {};
__export(pisellAdjustPrice_exports, {
  default: () => pisellAdjustPrice_default
});
module.exports = __toCommonJS(pisellAdjustPrice_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_classnames = __toESM(require("classnames"));
var import_utils = require("@pisell/utils");
var import_pisellText = __toESM(require("../pisellText"));
var import_Amount = __toESM(require("../virtual-keyboard/Amount"));
var import_locales = require("../../locales");
var import_index = require("./index.less");
var import_usePisellConfig = __toESM(require("../pisell-config-provider/hooks/usePisellConfig"));
var import_status = require("./status");
var PisellAdjustPrice = (props) => {
  const {
    value,
    onChange,
    defaultValue,
    originValue,
    selectType = "light",
    resetText,
    doneText,
    ...others
  } = props;
  const config = (0, import_usePisellConfig.default)();
  const [newValue, setNewValue] = (0, import_react.useState)(value || 0);
  const [updateMode, setUpdateMode] = (0, import_react.useState)(
    "priceOverride"
  );
  const [discountMode, setDiscountMode] = (0, import_react.useState)(
    "amount"
  );
  (0, import_react.useEffect)(() => {
    document.body.id = "body";
  }, []);
  const configColor = (0, import_react.useMemo)(() => {
    const currentConfig = import_status.defaultConfig[selectType];
    const validProps = Object.entries(props).reduce((acc, [key, value2]) => {
      if (value2 !== void 0 && key in currentConfig) {
        acc[key] = value2;
      }
      return acc;
    }, {});
    return { ...currentConfig, ...validProps };
  }, [selectType, props]);
  const handleEnter = () => {
    if (isDiscountError) {
      return;
    }
    if (updateMode === "priceOverride") {
      onChange == null ? void 0 : onChange(Number(newValue));
    } else {
      onChange == null ? void 0 : onChange(Number(totalAfterDiscount));
    }
  };
  const handleChange = (val) => {
    setNewValue(val);
  };
  const totalAfterDiscount = (0, import_react.useMemo)(() => {
    if (discountMode === "amount") {
      return originValue - Number(newValue || 0);
    }
    return originValue - originValue * (Number(newValue || 0) / 100);
  }, [originValue, newValue, discountMode]);
  const isDiscountError = (0, import_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, import_react.useEffect)(() => {
    (0, import_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__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("pisell-lowcode-adjust-price-wrap", {
        "pisell-lowcode-adjust-price-dark": selectType === "dark",
        "pisell-lowcode-adjust-price-light": selectType === "light"
      })
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Segmented,
      {
        size: "large",
        options: [
          {
            label: (0, import_locales.getText)("pisell-adjust-price-price-override"),
            value: "priceOverride"
          },
          { label: (0, import_locales.getText)("pisell-adjust-price-discount"), value: "discount" }
        ],
        value: updateMode,
        onChange: handleUpdateMode,
        block: true
      }
    ),
    updateMode === "discount" && /* @__PURE__ */ import_react.default.createElement(
      import_antd.Segmented,
      {
        size: "large",
        options: [
          {
            label: `${(0, import_locales.getText)("pisell-adjust-price-discount-amount")}(${(config == null ? void 0 : config.symbol) || "$"})`,
            value: "amount"
          },
          {
            label: `${(0, import_locales.getText)("pisell-adjust-price-discount-percentage")}(%)`,
            value: "percentage"
          }
        ],
        value: discountMode,
        onChange: (val) => {
          setNewValue("");
          setDiscountMode(val);
        },
        block: true
      }
    ),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-adjust-price-amount-wrap" }, /* @__PURE__ */ import_react.default.createElement(
      import_Amount.default,
      {
        ...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__ */ import_react.default.createElement("div", null, configColor.isShowMainText ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-low-code-adjust-price-total" }, isDiscountError ? /* @__PURE__ */ import_react.default.createElement(import_pisellText.default.Amount, { value: originValue }) : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, newValue && (0, import_locales.getText)("pisell-adjust-price-total-after-discount"), /* @__PURE__ */ import_react.default.createElement(import_pisellText.default.Amount, { value: totalAfterDiscount }))) : null, configColor.isShowSubText ? /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(
          "pisell-low-code-adjust-price-amount-desc",
          {
            ["pisell-low-code-adjust-price-amount-desc-error"]: isDiscountError
          }
        )
      },
      (0, import_locales.getText)("pisell-adjust-price-discount-can-be-maximum"),
      " ",
      discountMode === "amount" ? /* @__PURE__ */ import_react.default.createElement(
        import_pisellText.default.Amount,
        {
          value: originValue,
          style: {
            color: configColor.subTextColor
          }
        }
      ) : "100%"
    ) : null)
  );
};
var pisellAdjustPrice_default = PisellAdjustPrice;
