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/pro/priceKeyboard/components/cashKeyboard/CashKeyboardSimple.tsx
var CashKeyboardSimple_exports = {};
__export(CashKeyboardSimple_exports, {
  default: () => CashKeyboardSimple_default
});
module.exports = __toCommonJS(CashKeyboardSimple_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_decimal = __toESM(require("decimal.js"));
var import_lodash = require("lodash");
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_shortcuts = __toESM(require("../shortcuts"));
var import_rightItems = __toESM(require("../../hooks/rightItems"));
var import_keyActions = __toESM(require("../../hooks/keyActions"));
var import_utils2 = require("../../utils");
var import_index = require("./index.less");
var CashKeyboardBase = (props) => {
  var _a, _b, _c;
  const {
    amountSymbol,
    titleArea,
    originalValue = "",
    displayField,
    presets,
    keyboardProps,
    style,
    min,
    max,
    selectType = "light",
    placeholder = "",
    doneText,
    resetText
  } = props;
  const [internalValue, setInternalValue] = (0, import_ahooks.useControllableValue)(props);
  const uniqueId = (0, import_react.useId)();
  const uniqueKeyboardId = `virtual-keyboard-${uniqueId.replace(/:/g, "-")}`;
  const shortcutsRef = (0, import_react.useRef)(null);
  const [isFirstInput, setIsFirstInput] = (0, import_react.useState)(true);
  const changeDue = (0, import_react.useMemo)(() => {
    return Math.max(
      0,
      new import_decimal.default(internalValue || 0).minus(new import_decimal.default(originalValue || 0)).toNumber()
    );
  }, [internalValue, originalValue]);
  (0, import_react.useEffect)(() => {
    (0, import_utils2.setKeyboardTheme)(selectType);
  }, [selectType]);
  const onReset = (0, import_react.useCallback)(() => {
    var _a2;
    setInternalValue(String(originalValue));
    setIsFirstInput(true);
    (_a2 = props == null ? void 0 : props.onReset) == null ? void 0 : _a2.call(props);
  }, [props == null ? void 0 : props.onReset, setInternalValue, originalValue]);
  const _handleValChange = (0, import_react.useCallback)((val) => {
    setInternalValue(val);
    setIsFirstInput(false);
  }, []);
  const { handleChange } = (0, import_keyActions.default)({
    onEnter: props.onEnter,
    onReset,
    onChange: _handleValChange,
    value: internalValue,
    min,
    max,
    scale: 2,
    mode: isFirstInput ? "replace" : "append"
  });
  const rightItems = (0, import_rightItems.default)({
    rightItemsStyle: {},
    doneText: doneText || import_utils.locales.getText("cash-keyboard-pay"),
    resetText: resetText || import_utils.locales.getText("cash-keyboard-reset")
  });
  const getDefaultPresets = (0, import_react.useCallback)(() => {
    const userPresets = presets || [];
    const limitedPresets = userPresets.slice(0, 4);
    while (limitedPresets.length < 4) {
      limitedPresets.push({
        label: "",
        value: "",
        style: {
          visibility: "hidden",
          pointerEvents: "none"
        }
      });
    }
    return limitedPresets;
  }, [presets]);
  const getDefaultDisplayField = (0, import_react.useCallback)(() => {
    return {
      cashReceived: {
        visible: true,
        label: import_utils.locales.getText("cash-keyboard-cash-received")
      },
      balanceDue: {
        visible: true,
        label: import_utils.locales.getText("cash-keyboard-balance-due")
      },
      changeDue: {
        visible: true,
        label: import_utils.locales.getText("cash-keyboard-change-due")
      },
      gridRatio: [2, 1, 1]
      // Cash Received 占 2 份，其他各占 1 份
    };
  }, []);
  const defaultTitleArea = {
    title: {
      visible: true
    },
    subtitle: {
      visible: true
    },
    alignment: "space-between",
    padding: "0px"
  };
  const mergedTitleArea = (0, import_react.useMemo)(
    () => (0, import_lodash.merge)({}, defaultTitleArea, titleArea),
    [titleArea]
  );
  const mergedDisplayField = (0, import_react.useMemo)(
    () => (0, import_lodash.merge)({}, getDefaultDisplayField(), displayField),
    [getDefaultDisplayField, displayField]
  );
  const renderTitleArea = (0, import_react.useCallback)(() => {
    const { title, subtitle } = mergedTitleArea || {};
    if (!(title == null ? void 0 : title.visible) && !(subtitle == null ? void 0 : subtitle.visible)) {
      return null;
    }
    if (!(title == null ? void 0 : title.text) && !(subtitle == null ? void 0 : subtitle.text)) {
      return null;
    }
    const titleStyle = {
      display: "flex",
      justifyContent: mergedTitleArea.alignment,
      alignItems: "center",
      padding: mergedTitleArea.padding
    };
    return /* @__PURE__ */ import_react.default.createElement("div", { style: titleStyle, className: "pisell-cash-keyboard-title" }, (title == null ? void 0 : title.visible) && /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: {
          fontSize: title.fontSize,
          color: title.color,
          fontWeight: title.fontWeight
        }
      },
      title.text
    ), (subtitle == null ? void 0 : subtitle.visible) && /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "pisell-cash-keyboard-subtitle",
        style: {
          fontSize: subtitle.fontSize,
          color: subtitle.color,
          fontWeight: subtitle.fontWeight
        }
      },
      mergedTitleArea.subtitle.text
    ));
  }, [mergedTitleArea]);
  const getGridStyle = (0, import_react.useCallback)(
    (index) => {
      if (!mergedDisplayField.gridRatio) {
        return { flex: 1 };
      }
      const ratio = mergedDisplayField.gridRatio[index] || 1;
      return { flex: ratio };
    },
    [mergedDisplayField]
  );
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "pisell-cash-keyboard pisell-cash-keyboard-simple",
      style: { ...style || {} }
    },
    renderTitleArea(),
    /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "flex", gap: "8px" } }, ((_a = mergedDisplayField.cashReceived) == null ? void 0 : _a.visible) && /* @__PURE__ */ import_react.default.createElement(
      import_materials.VirtualInput,
      {
        focused: true,
        wrapperStyle: {
          padding: "12px 20px",
          borderRadius: "8px",
          backgroundColor: "white",
          boxShadow: "0px 1px 2px rgba(16, 24, 40, 0.06)",
          ...getGridStyle(0),
          overflow: "hidden"
        },
        topContent: /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            style: {
              color: "#667085",
              fontSize: "12px",
              fontWeight: 500,
              lineHeight: "18px"
            }
          },
          mergedDisplayField.cashReceived.label
        ),
        value: internalValue ? `${amountSymbol || ""}${internalValue}` : "",
        placeholder,
        style: {
          fontSize: "24px",
          fontWeight: "bold",
          border: "none",
          background: "transparent",
          width: "100%"
        },
        preventBlurElements: [`#${uniqueKeyboardId}`, shortcutsRef],
        selected: isFirstInput && !!internalValue
      }
    ), /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: {
          display: "flex",
          flexDirection: "column",
          flex: 1,
          borderRadius: "8px",
          backgroundColor: "#ffffff",
          padding: 8,
          boxShadow: "0px 1px 2px rgba(16, 24, 40, 0.06)",
          overflow: "hidden"
        }
      },
      ((_b = mergedDisplayField.balanceDue) == null ? void 0 : _b.visible) && /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          style: {
            ...getGridStyle(1)
          }
        },
        /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            style: {
              color: "#667085",
              fontSize: "12px",
              fontWeight: 500,
              lineHeight: "18px"
            }
          },
          mergedDisplayField.balanceDue.label
        ),
        /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            style: {
              color: "#475467",
              fontSize: "14px",
              fontWeight: 700,
              lineHeight: "20px"
            }
          },
          /* @__PURE__ */ import_react.default.createElement(
            import_materials.PisellText.Amount,
            {
              value: originalValue,
              hideDecimalForWholeNumbers: false,
              precision: 2
            }
          )
        )
      ),
      ((_c = mergedDisplayField.changeDue) == null ? void 0 : _c.visible) && /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          style: {
            ...getGridStyle(2)
          }
        },
        /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            style: {
              color: "#667085",
              fontSize: "12px",
              fontWeight: 500,
              lineHeight: "18px"
            }
          },
          mergedDisplayField.changeDue.label
        ),
        /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            style: {
              color: "#475467",
              fontSize: "14px",
              fontWeight: 700,
              lineHeight: "20px"
            }
          },
          /* @__PURE__ */ import_react.default.createElement(
            import_materials.PisellText.Amount,
            {
              value: changeDue,
              hideDecimalForWholeNumbers: false,
              precision: 2
            }
          )
        )
      )
    )),
    /* @__PURE__ */ import_react.default.createElement(
      import_shortcuts.default,
      {
        ref: shortcutsRef,
        presets: getDefaultPresets(),
        onChange: (v) => {
          setInternalValue(v);
        }
      }
    ),
    /* @__PURE__ */ import_react.default.createElement(
      import_materials.Keyboard,
      {
        ...keyboardProps,
        rightItems,
        onChange: handleChange,
        id: uniqueKeyboardId
      }
    )
  );
};
var CashKeyboardSimple = (props) => {
  const { containerProps, children, ...keyboardProps } = props;
  const { renderType, open, ...restProps } = containerProps || {};
  if (!containerProps) {
    return /* @__PURE__ */ import_react.default.createElement(CashKeyboardBase, { ...keyboardProps });
  }
  if (renderType === "popover") {
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Popover,
      {
        key: `${open}`,
        open,
        trigger: "click",
        className: "pisell-cash-keyboard-container pisell-cash-keyboard-popover",
        ...restProps,
        content: /* @__PURE__ */ import_react.default.createElement(CashKeyboardBase, { ...keyboardProps })
      },
      children
    );
  }
  if (renderType === "modal") {
    return open ? /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellModal,
      {
        platform: "pc",
        open: true,
        width: "unset",
        header: null,
        footer: false,
        mask: false,
        maskClosable: true,
        bodyStyle: { padding: "0px", backgroundColor: "transparent" },
        closeIcon: null,
        className: "pisell-payment-keyboard-container pisell-payment-keyboard-modal",
        destroyOnHidden: true,
        ...restProps
      },
      /* @__PURE__ */ import_react.default.createElement(CashKeyboardBase, { ...keyboardProps })
    ) : null;
  }
  return /* @__PURE__ */ import_react.default.createElement(CashKeyboardBase, { ...keyboardProps });
};
var CashKeyboardSimple_default = CashKeyboardSimple;
