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, { useState } from 'react';
import { Iconfont } from '@pisell/materials';
import { NumberKeyboard } from "../../../keyboards";
import { formatWeight, sanitizeManualWeight } from "./utils";
export function TareQuickBar(_ref) {
  var options = _ref.options,
    selected = _ref.selected,
    compact = _ref.compact,
    unitLabel = _ref.unitLabel,
    convertTareValue = _ref.convertTareValue,
    onSelect = _ref.onSelect;
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__tare-quick-bar".concat(compact ? ' pisell-sku-detail-weighing__tare-quick-bar--compact' : ' pisell-sku-detail-weighing__tare-quick-bar--expanded')
  }, options.map(function (opt) {
    var active = opt.id === selected;
    var displayValue = convertTareValue(opt);
    var showValue = !compact && opt.id !== 'none';
    return /*#__PURE__*/React.createElement("button", {
      key: opt.id,
      type: "button",
      className: "pisell-sku-detail-weighing__tare-chip".concat(active ? ' pisell-sku-detail-weighing__tare-chip--active' : '').concat(showValue ? '' : ' pisell-sku-detail-weighing__tare-chip--no-value'),
      onClick: function onClick() {
        return onSelect(opt.id);
      },
      "aria-pressed": active
    }, /*#__PURE__*/React.createElement("span", {
      className: "pisell-sku-detail-weighing__tare-chip-label"
    }, opt.label), showValue && /*#__PURE__*/React.createElement("span", {
      className: "pisell-sku-detail-weighing__tare-chip-value"
    }, formatWeight(displayValue), "\u2009", unitLabel));
  }));
}
export function TareManagementPanel(props) {
  var componentStyle = props.componentStyle,
    tareOptions = props.tareOptions,
    selectedTare = props.selectedTare,
    customTareIdSet = props.customTareIdSet,
    unitLabel = props.unitLabel,
    maxTareValue = props.maxTareValue,
    convertTareValue = props.convertTareValue,
    getText = props.getText,
    onSelectTare = props.onSelectTare,
    onDeleteCustomTare = props.onDeleteCustomTare,
    onAddCustomTare = props.onAddCustomTare;
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    newTareLabel = _useState2[0],
    setNewTareLabel = _useState2[1];
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    newTareValue = _useState4[0],
    setNewTareValue = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    newTareKeyboardOpen = _useState6[0],
    setNewTareKeyboardOpen = _useState6[1];
  var trimmedNewTareLabel = newTareLabel.trim();
  var normalizedNewTareLabel = trimmedNewTareLabel.toLocaleLowerCase();
  var isDuplicateTareLabel = normalizedNewTareLabel !== '' && tareOptions.some(function (item) {
    return item.label.trim().toLocaleLowerCase() === normalizedNewTareLabel;
  });
  var parsedNewTareValue = newTareValue === '' ? Number.NaN : Number(newTareValue);
  var isAddDisabled = !trimmedNewTareLabel || isDuplicateTareLabel || !Number.isFinite(parsedNewTareValue) || parsedNewTareValue < 0 || parsedNewTareValue > maxTareValue;
  var handleNewTareValueChange = function handleNewTareValueChange(value) {
    var nextValue = sanitizeManualWeight(value);
    if (!nextValue) {
      setNewTareValue('');
      return;
    }
    var parsedValue = Number(nextValue);
    if (!Number.isFinite(parsedValue) || parsedValue > maxTareValue) {
      return;
    }
    setNewTareValue(nextValue);
  };
  var handleNewTareKeyboardConfirm = function handleNewTareKeyboardConfirm(value) {
    handleNewTareValueChange(value);
    setNewTareKeyboardOpen(false);
  };
  var handleAddCustomTare = function handleAddCustomTare() {
    if (isAddDisabled) {
      return;
    }
    var didAdd = onAddCustomTare({
      label: newTareLabel,
      value: parsedNewTareValue
    });
    if (!didAdd) {
      return;
    }
    setNewTareLabel('');
    setNewTareValue('');
    setNewTareKeyboardOpen(false);
  };
  var handleAddCustomTareInputKeyDown = function handleAddCustomTareInputKeyDown(event) {
    if (event.key !== 'Enter') {
      return;
    }
    event.preventDefault();
    if (isAddDisabled) {
      return;
    }
    handleAddCustomTare();
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-panel",
    style: componentStyle
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-group"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-group-header"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-label"
  }, getText('pisell2.text.sku.detail.modal.weighing.metric.tare', 'Tare')), /*#__PURE__*/React.createElement("button", {
    type: "button",
    onClick: function onClick() {
      return onSelectTare('none');
    },
    className: "pisell-sku-detail-weighing__settings-link"
  }, getText('pisell2.text.sku.detail.modal.weighing.settings.clear', 'Clear'))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-subtitle"
  }, getText('pisell2.text.sku.detail.modal.weighing.settings.tare.subtitle', 'If the reading is tared, please choose "No Tare".')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-tare-list"
  }, tareOptions.map(function (item) {
    var active = selectedTare === item.id;
    var isCustom = customTareIdSet.has(item.id);
    var tareValueForDisplay = convertTareValue(item);
    return /*#__PURE__*/React.createElement("div", {
      key: item.id,
      className: "pisell-sku-detail-weighing__tare-option".concat(active ? ' pisell-sku-detail-weighing__tare-option--active' : ''),
      onClick: function onClick() {
        return onSelectTare(item.id);
      },
      onKeyDown: function onKeyDown(event) {
        if (event.key === 'Enter' || event.key === ' ') {
          event.preventDefault();
          onSelectTare(item.id);
        }
      },
      role: "button",
      tabIndex: 0,
      "aria-pressed": active
    }, /*#__PURE__*/React.createElement("span", {
      className: "pisell-sku-detail-weighing__tare-option-content"
    }, /*#__PURE__*/React.createElement("span", {
      className: "pisell-sku-detail-weighing__tare-option-label"
    }, item.label), /*#__PURE__*/React.createElement("span", {
      className: "pisell-sku-detail-weighing__tare-option-value"
    }, "".concat(formatWeight(tareValueForDisplay), " ").concat(unitLabel))), isCustom ? /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "pisell-sku-detail-weighing__tare-delete",
      onClick: function onClick(event) {
        event.stopPropagation();
        onDeleteCustomTare(item.id);
      },
      "aria-label": getText('pisell2.text.sku.detail.modal.weighing.settings.delete.tare', 'Delete tare')
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-trash-01",
      size: 14
    })) : null);
  }))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-group"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-label"
  }, getText('pisell2.text.sku.detail.modal.weighing.settings.add.tare', 'Add Tare')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-form"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-sku-detail-weighing__settings-form-row"
  }, /*#__PURE__*/React.createElement("input", {
    value: newTareLabel,
    onChange: function onChange(event) {
      return setNewTareLabel(event.target.value);
    },
    onKeyDown: handleAddCustomTareInputKeyDown,
    placeholder: getText('pisell2.text.sku.detail.modal.weighing.settings.label.placeholder', 'Label'),
    className: "pisell-sku-detail-weighing__settings-input".concat(isDuplicateTareLabel ? ' pisell-sku-detail-weighing__settings-input--error' : ''),
    "aria-invalid": isDuplicateTareLabel
  }), /*#__PURE__*/React.createElement(NumberKeyboard, {
    defaultValue: newTareValue,
    originalValue: newTareValue,
    mode: "decimal",
    min: 0,
    max: maxTareValue,
    precision: 3,
    onEnter: handleNewTareKeyboardConfirm,
    containerProps: {
      className: 'pisell-sku-detail-weighing__keyboard-popover',
      renderType: 'popover',
      placement: 'bottom',
      open: newTareKeyboardOpen,
      onOpenChange: setNewTareKeyboardOpen
    }
  }, /*#__PURE__*/React.createElement("input", {
    value: newTareValue,
    onClick: function onClick() {
      return setNewTareKeyboardOpen(true);
    },
    onFocus: function onFocus() {
      return setNewTareKeyboardOpen(true);
    },
    onKeyDown: handleAddCustomTareInputKeyDown,
    placeholder: unitLabel,
    inputMode: "decimal",
    className: "pisell-sku-detail-weighing__settings-input pisell-sku-detail-weighing__settings-input--readonly",
    readOnly: true
  })), /*#__PURE__*/React.createElement("button", {
    type: "button",
    onClick: handleAddCustomTare,
    className: "pisell-sku-detail-weighing__settings-action",
    disabled: isAddDisabled
  }, getText('pisell2.text.sku.detail.modal.weighing.settings.add', 'Add'))))));
}