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); }
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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
import React, { useEffect, useMemo, useState } from 'react';
import { Tooltip } from 'antd';
import { useControllableValue } from 'ahooks';
//@ts-ignore
import { merge, omit } from 'lodash';
//@ts-ignore
import { locales } from '@pisell/utils';
import Shortcuts from "../shortcuts";
import CashKeyboardSimple from "./CashKeyboardSimple";
import { setKeyboardTheme } from "../../utils";
import "./index.less";
import Decimal from 'decimal.js';
import { AutoResizeText } from '@pisell/materials';
import { formatAmountWithOptions } from '@pisell/utils';
import { Iconfont } from '@pisell/materials';
var CashKeyboardIntact = function CashKeyboardIntact(props) {
  var _mergedDisplayField$b, _mergedDisplayField$c, _mergedDisplayField$c2;
  var amountSymbol = props.amountSymbol,
    _props$titleArea = props.titleArea,
    titleArea = _props$titleArea === void 0 ? {} : _props$titleArea,
    _props$displayField = props.displayField,
    displayField = _props$displayField === void 0 ? {} : _props$displayField,
    originalValue = props.originalValue,
    presets = props.presets,
    style = props.style,
    _props$selectType = props.selectType,
    selectType = _props$selectType === void 0 ? 'light' : _props$selectType,
    placeholder = props.placeholder,
    doneText = props.doneText,
    resetText = props.resetText,
    disabled = props.disabled,
    hiddenChangeDue = props.hiddenChangeDue,
    shortcutsSize = props.shortcutsSize;
  var _useControllableValue = useControllableValue(props),
    _useControllableValue2 = _slicedToArray(_useControllableValue, 2),
    internalValue = _useControllableValue2[0],
    setInternalValue = _useControllableValue2[1];
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isShowModal = _useState2[0],
    setIsShowModal = _useState2[1];

  // 找零
  var changeDue = useMemo(function () {
    return Math.max(0, new Decimal(internalValue || 0).minus(new Decimal(originalValue || 0)).toNumber());
  }, [internalValue, originalValue]);
  var _restProps = useMemo(function () {
    return omit(props, ['defaultValue', 'value', 'onChange', 'onEnter', 'onReset', 'style']);
  }, [props]);

  // 设置主题
  useEffect(function () {
    setKeyboardTheme(selectType);
  }, [selectType]);

  // 变体2：末尾有三个默认预设项，总数不大于7个(因为支付占了 2)
  var getDefaultPresets = function getDefaultPresets() {
    var defaultEndPresets = [{
      label: locales.getText('cash-keyboard-other'),
      value: 'other',
      style: {
        color: '#344054',
        backgroundColor: 'white'
      },
      onClick: function onClick() {
        setIsShowModal(true);
      },
      render: function render() {
        return /*#__PURE__*/React.createElement("div", {
          style: {
            position: 'absolute',
            left: 0,
            top: 0,
            bottom: 0,
            right: 0,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center'
          }
        }, locales.getText('cash-keyboard-other'));
      }
    }, {
      label: resetText || locales.getText('cash-keyboard-reset'),
      value: 'reset',
      style: {
        color: '#D92D20',
        backgroundColor: 'white'
      },
      onClick: function onClick() {
        var _props$onReset;
        // 重置为待支付金额
        setInternalValue(String(originalValue) || '');
        props === null || props === void 0 || (_props$onReset = props.onReset) === null || _props$onReset === void 0 || _props$onReset.call(props);
      }
    }, {
      label: doneText || locales.getText('cash-keyboard-pay'),
      value: 'pay',
      style: {
        color: 'white',
        backgroundColor: '#7F56DA',
        gridColumn: 'span 2'
      },
      onClick: function onClick() {
        var _props$onEnter;
        (_props$onEnter = props.onEnter) === null || _props$onEnter === void 0 || _props$onEnter.call(props, internalValue);
      }
    }];
    var userPresets = presets || [];

    // 计算可用的用户预设数量（总数7 - 默认3个 = 4个）
    var maxUserPresets = 7 - defaultEndPresets.length;
    var limitedUserPresets = userPresets.slice(0, maxUserPresets);
    return [].concat(_toConsumableArray(limitedUserPresets), defaultEndPresets);
  };

  // 变体2：左中右布局，中间为Input区域
  var getDefaultDisplayField = function getDefaultDisplayField() {
    return {
      cashReceived: {
        visible: true,
        label: locales.getText('cash-keyboard-cash-received')
      },
      balanceDue: {
        visible: true,
        label: locales.getText('cash-keyboard-balance-due')
      },
      changeDue: {
        visible: true,
        label: locales.getText('cash-keyboard-change-due')
      },
      gridRatio: [1, 2, 1] // 左中右布局，中间占 2 份
    };
  };

  // 默认配置
  var defaultTitleArea = {
    visible: true,
    title: {
      visible: true
    },
    subtitle: {
      visible: true
    },
    alignment: 'space-between',
    padding: '0px'
  };

  // 合并配置
  var mergedTitleArea = merge({}, defaultTitleArea, titleArea);
  var mergedDisplayField = merge({}, getDefaultDisplayField(), displayField);

  // 渲染标题区域
  var renderTitleArea = function renderTitleArea() {
    var _mergedTitleArea$titl, _mergedTitleArea$subt;
    var _ref = mergedTitleArea || {},
      title = _ref.title,
      subtitle = _ref.subtitle;
    if (!(title !== null && title !== void 0 && title.visible) && !(subtitle !== null && subtitle !== void 0 && subtitle.visible)) {
      return null;
    }
    if (!(title !== null && title !== void 0 && title.text) && !(subtitle !== null && subtitle !== void 0 && subtitle.text)) {
      return null;
    }
    var titleStyle = {
      display: 'flex',
      justifyContent: mergedTitleArea.alignment,
      alignItems: 'center',
      padding: mergedTitleArea.padding,
      marginBottom: '8'
    };
    return /*#__PURE__*/React.createElement("div", {
      style: titleStyle
    }, ((_mergedTitleArea$titl = mergedTitleArea.title) === null || _mergedTitleArea$titl === void 0 ? void 0 : _mergedTitleArea$titl.visible) && /*#__PURE__*/React.createElement("div", {
      className: "pisell-cash-keyboard-title",
      style: {
        fontSize: mergedTitleArea.title.fontSize,
        color: mergedTitleArea.title.color,
        fontWeight: mergedTitleArea.title.fontWeight
      }
    }, mergedTitleArea.title.text), ((_mergedTitleArea$subt = mergedTitleArea.subtitle) === null || _mergedTitleArea$subt === void 0 ? void 0 : _mergedTitleArea$subt.visible) && /*#__PURE__*/React.createElement("div", {
      className: "pisell-cash-keyboard-subtitle",
      style: {
        fontSize: mergedTitleArea.subtitle.fontSize,
        color: mergedTitleArea.subtitle.color,
        fontWeight: mergedTitleArea.subtitle.fontWeight
      }
    }, mergedTitleArea.subtitle.text));
  };

  // 计算栅格样式
  var getGridStyle = function getGridStyle(index) {
    if (!mergedDisplayField.gridRatio) {
      return {
        flex: 1
      };
    }
    var ratio = mergedDisplayField.gridRatio[index] || 1;
    return {
      flex: ratio
    };
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard pisell-cash-keyboard-intact ".concat(disabled ? 'disabled' : ''),
    style: _objectSpread({}, style || {})
  }, renderTitleArea(), /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container"
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex',
      gap: '16px'
    }
  }, ((_mergedDisplayField$b = mergedDisplayField.balanceDue) === null || _mergedDisplayField$b === void 0 ? void 0 : _mergedDisplayField$b.visible) && /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container-top",
    style: _objectSpread(_objectSpread({}, getGridStyle(0)), {}, {
      overflow: 'hidden'
    })
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container-top-label"
  }, mergedDisplayField.balanceDue.label), /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container-top-value"
  }, /*#__PURE__*/React.createElement(Tooltip, {
    title: formatAmountWithOptions(originalValue || '', amountSymbol, {
      precision: 2,
      hideDecimalForWholeNumbers: false,
      showCurrencySymbol: true
    })
  }, /*#__PURE__*/React.createElement(AutoResizeText, {
    minFontSizePx: 10,
    maxFontSizePx: 18,
    fontSizePrecisionPx: 0.5
  }, formatAmountWithOptions(originalValue || '', amountSymbol, {
    precision: 2,
    hideDecimalForWholeNumbers: false,
    showCurrencySymbol: true
  }))))), ((_mergedDisplayField$c = mergedDisplayField.cashReceived) === null || _mergedDisplayField$c === void 0 ? void 0 : _mergedDisplayField$c.visible) && /*#__PURE__*/React.createElement("div", {
    style: _objectSpread(_objectSpread({}, getGridStyle(1)), {}, {
      padding: '0px 16px',
      borderRadius: '8px',
      backgroundColor: 'white',
      boxShadow: 'inset 0 2px 4px rgba(16, 24, 40, 0.08), inset 0 -2px 4px rgba(16, 24, 40, 0.08), inset 2px 0 4px rgba(16, 24, 40, 0.08), inset -2px 0 4px rgba(16, 24, 40, 0.08)',
      overflow: 'hidden',
      maxWidth: 188,
      cursor: 'pointer',
      border: '1px solid #7f56da',
      borderBottom: '1px solid #7f56da'
    }),
    onClick: function onClick() {
      setIsShowModal(true);
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container-top",
    style: _objectSpread(_objectSpread({}, getGridStyle(0)), {}, {
      overflow: 'hidden',
      padding: '5px 0'
    })
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container-top-label"
  }, mergedDisplayField.cashReceived.label), /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container-top-value"
  }, /*#__PURE__*/React.createElement(AutoResizeText, {
    minFontSizePx: 10,
    maxFontSizePx: 18,
    fontSizePrecisionPx: 0.5,
    style: {
      color: '#7f56da',
      textDecoration: 'underline',
      textUnderlineOffset: '3px'
    }
  }, internalValue ? formatAmountWithOptions(internalValue || '', amountSymbol, {
    value: internalValue,
    hideDecimalForWholeNumbers: false,
    precision: 2,
    showCurrencySymbol: true
  }) : ''), /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-edit-05",
    style: {
      color: '#7f56da',
      fontSize: '18px',
      marginLeft: '4px',
      marginTop: '4px'
    }
  })))), ((_mergedDisplayField$c2 = mergedDisplayField.changeDue) === null || _mergedDisplayField$c2 === void 0 ? void 0 : _mergedDisplayField$c2.visible) && /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container-top",
    style: _objectSpread(_objectSpread({}, getGridStyle(2)), {}, {
      overflow: 'hidden'
    })
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container-top-label"
  }, mergedDisplayField.changeDue.label), /*#__PURE__*/React.createElement("div", {
    className: "pisell-cash-keyboard-intact-container-top-value"
  }, /*#__PURE__*/React.createElement(Tooltip, {
    title: formatAmountWithOptions(hiddenChangeDue ? 0 : changeDue, amountSymbol, {
      value: hiddenChangeDue ? 0 : changeDue,
      showCurrencySymbol: true,
      hideDecimalForWholeNumbers: false,
      precision: 2
    })
  }, /*#__PURE__*/React.createElement(AutoResizeText, {
    minFontSizePx: 10,
    maxFontSizePx: 18,
    fontSizePrecisionPx: 0.5
  }, formatAmountWithOptions(hiddenChangeDue ? 0 : changeDue, amountSymbol, {
    value: hiddenChangeDue ? 0 : changeDue,
    showCurrencySymbol: true,
    hideDecimalForWholeNumbers: false,
    precision: 2
  })))))), /*#__PURE__*/React.createElement(Shortcuts, {
    size: shortcutsSize || 'default',
    presets: getDefaultPresets(),
    onChange: setInternalValue,
    disabled: disabled
  }))), /*#__PURE__*/React.createElement(CashKeyboardSimple, _extends({
    containerProps: {
      renderType: 'modal',
      open: isShowModal,
      onCancel: function onCancel() {
        return setIsShowModal(false);
      }
    }
  }, _restProps, {
    titleArea: _objectSpread(_objectSpread({}, mergedTitleArea), {}, {
      subtitle: {
        visible: false
      }
    }),
    selectType: "dark",
    value: internalValue,
    onChange: setInternalValue,
    onEnter: function onEnter() {
      var _props$onEnter2;
      (_props$onEnter2 = props.onEnter) === null || _props$onEnter2 === void 0 || _props$onEnter2.call(props, internalValue);
      setIsShowModal(false);
    }
  })));
};
export default CashKeyboardIntact;