function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["value", "onChange", "defaultValue", "originValue", "selectType", "resetText", "doneText"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useEffect, useMemo, useState } from 'react';
import { Segmented } from 'antd';
import classNames from 'classnames';
import { setTheme } from '@pisell/utils';
import PisellText from "../pisellText";
import Amount from "../virtual-keyboard/Amount";
import { getText } from "../../locales";
import "./index.less";
import usePisellConfig from "../pisell-config-provider/hooks/usePisellConfig";
import { defaultConfig } from "./status";
var PisellAdjustPrice = function PisellAdjustPrice(props) {
  var value = props.value,
    onChange = props.onChange,
    defaultValue = props.defaultValue,
    originValue = props.originValue,
    _props$selectType = props.selectType,
    selectType = _props$selectType === void 0 ? 'light' : _props$selectType,
    resetText = props.resetText,
    doneText = props.doneText,
    others = _objectWithoutProperties(props, _excluded);
  var config = usePisellConfig();
  var _useState = useState(value || 0),
    _useState2 = _slicedToArray(_useState, 2),
    newValue = _useState2[0],
    setNewValue = _useState2[1];

  // 一口价或者折扣
  var _useState3 = useState('priceOverride'),
    _useState4 = _slicedToArray(_useState3, 2),
    updateMode = _useState4[0],
    setUpdateMode = _useState4[1];

  // 折扣模式
  // 折扣模式分为 金额 和 百分比
  var _useState5 = useState('amount'),
    _useState6 = _slicedToArray(_useState5, 2),
    discountMode = _useState6[0],
    setDiscountMode = _useState6[1];
  useEffect(function () {
    document.body.id = 'body';
  }, []);

  /**
   * @title: 配置颜色
   * @description: 根据 props 和 selectType 生成最终的配置颜色
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-08-04 18:36
   */
  var configColor = useMemo(function () {
    var currentConfig = defaultConfig[selectType];

    // 过滤出有效的 props 并合并到默认配置中
    var validProps = Object.entries(props).reduce(function (acc, _ref) {
      var _ref2 = _slicedToArray(_ref, 2),
        key = _ref2[0],
        value = _ref2[1];
      if (value !== undefined && key in currentConfig) {
        acc[key] = value;
      }
      return acc;
    }, {});
    return _objectSpread(_objectSpread({}, currentConfig), validProps);
  }, [selectType, props]);
  var handleEnter = function handleEnter() {
    if (isDiscountError) {
      return;
    }

    // 一口价直接取newValue
    if (updateMode === 'priceOverride') {
      onChange === null || onChange === void 0 || onChange(Number(newValue));
    } else {
      // 折扣模式取折扣价格
      onChange === null || onChange === void 0 || onChange(Number(totalAfterDiscount));
    }
  };
  var handleChange = function handleChange(val) {
    setNewValue(val);
  };

  // 计算折扣后的价格
  var totalAfterDiscount = useMemo(function () {
    if (discountMode === 'amount') {
      return originValue - Number(newValue || 0);
    }
    return originValue - originValue * (Number(newValue || 0) / 100);
  }, [originValue, newValue, discountMode]);
  var isDiscountError = useMemo(function () {
    // 一口价时不展示折扣值
    if (updateMode === 'priceOverride') {
      return false;
    }
    // 折扣模式为金额时 输入金额不能大于原始值
    return totalAfterDiscount < 0;
  }, [updateMode, totalAfterDiscount]);
  var handleUpdateMode = function handleUpdateMode(val) {
    setUpdateMode(val);
    setNewValue(val === 'priceOverride' ? value || 0 : '');
  };
  var inputFormat = function inputFormat(v) {
    if (updateMode === 'priceOverride') {
      return null;
    }
    if (discountMode === 'amount') {
      return null;
    } else {
      return "".concat(v, "%");
    }
  };
  useEffect(function () {
    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: "".concat(getText('pisell-adjust-price-discount-amount'), "(").concat((config === null || config === void 0 ? void 0 : config.symbol) || '$', ")"),
      value: 'amount'
    }, {
      label: "".concat(getText('pisell-adjust-price-discount-percentage'), "(%)"),
      value: 'percentage'
    }],
    value: discountMode,
    onChange: function onChange(val) {
      setNewValue('');
      setDiscountMode(val);
    },
    block: true
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-adjust-price-amount-wrap"
  }, /*#__PURE__*/React.createElement(Amount, _extends({}, others, {
    selectType: selectType || 'light',
    inputFormat: inputFormat,
    defaultValue: updateMode === 'priceOverride' ? defaultValue : '',
    key: updateMode,
    value: newValue,
    defaultSelect: updateMode === 'priceOverride',
    onChange: handleChange,
    onEnter: handleEnter,
    placeholder: discountMode === 'amount' ? '0' : '%',
    resetText: resetText,
    doneText: 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', _defineProperty({}, '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));
};
export default PisellAdjustPrice;