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/pisellCashCounting/hooks.ts
var hooks_exports = {};
__export(hooks_exports, {
  usePisellCashCounting: () => usePisellCashCounting
});
module.exports = __toCommonJS(hooks_exports);
var import_react = require("react");
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_utils2 = require("./utils");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
function usePisellCashCounting({
  value,
  defaultValue = "",
  currencyCode,
  currencySymbol,
  amountPrecision,
  denominations = [],
  disabled = false,
  countDisabled = false,
  onChange,
  onCountConfirm,
  onCountCancel
}) {
  var _a, _b, _c, _d;
  const context = (0, import_useEngineContext.default)();
  const { store } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
  const modalRef = (0, import_react.useRef)(null);
  const triggerRef = (0, import_react.useRef)(null);
  const [keyboardOpen, setKeyboardOpen] = (0, import_react.useState)(false);
  const [internalValue, setInternalValue] = (0, import_react.useState)(defaultValue);
  const [confirmedQuantities, setConfirmedQuantities] = (0, import_react.useState)({});
  const countedResultRef = (0, import_react.useRef)(null);
  const manualInputValueRef = (0, import_react.useRef)(null);
  const [manualCountMismatchOpen, setManualCountMismatchOpen] = (0, import_react.useState)(false);
  const globalConfig = (0, import_react.useMemo)(() => {
    var _a2, _b2;
    const global = ((_b2 = (_a2 = store == null ? void 0 : store.getState) == null ? void 0 : _a2.call(store)) == null ? void 0 : _b2.global) || {};
    return {
      symbol: (global == null ? void 0 : global.symbol) || "$",
      currency_code: (global == null ? void 0 : global.currency_code) || "AUD"
    };
  }, [(_c = (_b = store == null ? void 0 : store.getState) == null ? void 0 : _b.call(store)) == null ? void 0 : _c.global]);
  const _currencyCode = (0, import_react.useMemo)(() => {
    return currencyCode || (globalConfig == null ? void 0 : globalConfig.currency_code);
  }, [currencyCode, globalConfig == null ? void 0 : globalConfig.currency_code]);
  const currencyDenominations = (0, import_react.useMemo)(() => {
    return (0, import_utils2.getEffectiveDenominationsForCurrency)(denominations, _currencyCode);
  }, [_currencyCode, denominations]);
  const decimalPlaces = (0, import_react.useMemo)(() => {
    var _a2;
    if (typeof amountPrecision === "number") {
      return amountPrecision;
    }
    return ((_a2 = currencyDenominations[0]) == null ? void 0 : _a2.decimal_places) ?? 2;
  }, [amountPrecision, (_d = currencyDenominations[0]) == null ? void 0 : _d.decimal_places]);
  const symbolValue = (0, import_react.useMemo)(() => {
    if (currencySymbol) {
      return currencySymbol;
    }
    return globalConfig == null ? void 0 : globalConfig.symbol;
  }, [currencySymbol, globalConfig == null ? void 0 : globalConfig.symbol]);
  const currentValue = (0, import_react.useMemo)(
    () => String(value ?? internalValue),
    [value, internalValue]
  );
  const updateAmount = (0, import_react.useCallback)(
    (nextValue, source) => {
      if (value === void 0) {
        setInternalValue(nextValue);
      }
      if (typeof onChange === "function") {
        onChange(nextValue, { source });
      }
    },
    [decimalPlaces, onChange, value]
  );
  const handleAmountChange = (0, import_react.useCallback)(
    (nextValue) => {
      manualInputValueRef.current = nextValue;
      updateAmount(String(nextValue), "manual");
    },
    [updateAmount]
  );
  const handleKeyboardClose = (0, import_react.useCallback)(
    () => {
      var _a2;
      setKeyboardOpen(false);
      if (((_a2 = countedResultRef.current) == null ? void 0 : _a2.amount) && Number(countedResultRef.current.amount) !== Number(manualInputValueRef.current)) {
        setManualCountMismatchOpen(true);
      }
    },
    [setManualCountMismatchOpen]
  );
  const confirmUseCountedTotal = (0, import_react.useCallback)(() => {
    var _a2;
    updateAmount(((_a2 = countedResultRef.current) == null ? void 0 : _a2.amount) ?? "", "count");
    setManualCountMismatchOpen(false);
  }, [updateAmount]);
  const confirmKeepManualAmount = (0, import_react.useCallback)(() => {
    countedResultRef.current = null;
    setConfirmedQuantities({});
    updateAmount(manualInputValueRef.current ?? "", "manual");
    setManualCountMismatchOpen(false);
  }, [updateAmount]);
  const openCountModal = (0, import_react.useCallback)(() => {
    var _a2;
    if (disabled || countDisabled) {
      return;
    }
    if (currencyDenominations.length === 0) {
      import_antd.message.warning(
        import_utils.locales.getText("pisell-cash-counting.no-country-config")
      );
      return;
    }
    const initialQuantities = {
      ...(0, import_utils2.buildEmptyQuantities)(currencyDenominations),
      ...confirmedQuantities
    };
    setKeyboardOpen(false);
    (_a2 = modalRef.current) == null ? void 0 : _a2.open({ initialQuantities });
  }, [confirmedQuantities, countDisabled, currencyDenominations, disabled]);
  const handleCountCancel = (0, import_react.useCallback)(() => {
    onCountCancel == null ? void 0 : onCountCancel();
  }, [onCountCancel]);
  const handleCountConfirm = (0, import_react.useCallback)(
    (payload) => {
      countedResultRef.current = payload;
      setConfirmedQuantities(payload.quantities);
      updateAmount(payload.amount, "count");
      if (typeof onCountConfirm === "function") {
        onCountConfirm(payload);
      }
    },
    [onCountConfirm, updateAmount]
  );
  return {
    modalRef,
    triggerRef,
    keyboardOpen,
    setKeyboardOpen,
    currencyDenominations,
    decimalPlaces,
    symbol: symbolValue,
    currencyCode: _currencyCode,
    currentValue,
    handleAmountChange,
    handleKeyboardClose,
    openCountModal,
    handleCountCancel,
    handleCountConfirm,
    manualCountMismatchOpen,
    confirmUseCountedTotal,
    confirmKeepManualAmount
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  usePisellCashCounting
});
