function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
var _excluded = ["value", "onChange", "defaultValue", "originValue"];
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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 _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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
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 PisellText from "../pisellText";
import Amount from "../virtual-keyboard/Amount";
import { getText } from "../../locales";
import "./index.less";
import usePisellConfig from "../pisell-config-provider/hooks/usePisellConfig";
var PisellAdjustPrice = function PisellAdjustPrice(props) {
  var value = props.value,
    onChange = props.onChange,
    defaultValue = props.defaultValue,
    originValue = props.originValue,
    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';
  }, []);
  var handleEnter = function handleEnter() {
    if (isDiscountError) {
      return;
    }

    // 一口价直接取newValue
    if (updateMode === 'priceOverride') {
      onChange === null || onChange === void 0 ? void 0 : onChange(Number(newValue));
    } else {
      // 折扣模式取折扣价格
      onChange === null || onChange === void 0 ? 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, "%");
    }
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-adjust-price-wrap"
  }, /*#__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, {
    inputFormat: inputFormat,
    defaultValue: updateMode === 'priceOverride' ? defaultValue : '',
    key: updateMode,
    value: newValue,
    defaultSelect: updateMode === 'priceOverride',
    onChange: handleChange,
    onEnter: handleEnter,
    placeholder: discountMode === 'amount' ? '0' : '%'
  }))), updateMode === 'discount' && /*#__PURE__*/React.createElement("div", null, /*#__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
  }))), /*#__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
  }) : '100%')));
};
export default PisellAdjustPrice;