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, useEffect } from 'react';
import { locales } from '@pisell/utils';
import { Iconfont, Button, Input } from '@pisell/materials';
import "./index.less";
import { useScannerDevice, useCameraDevice, useScanListener } from "./hooks";
export var NativeDeviceStatus = function NativeDeviceStatus(_ref) {
  var type = _ref.type,
    status = _ref.status;
  var iconType = type === 'scanner' ? 'pisell2-Scanner' : 'pisell2-camera';
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-status"
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: iconType,
    style: {
      fontSize: '40px'
    }
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-status-wrapper"
  }, /*#__PURE__*/React.createElement(Iconfont, {
    style: {
      borderRadius: '50%',
      background: status ? '#17B26A' : '#F04438',
      color: '#fff'
    },
    type: status ? 'pisell2-check' : 'pisell2-x-close'
  })));
};
var DEFAULT_SCANNER_CLASS = 'pisell-eftpos-pay-huifu-scan-content-scanner';
export var ScanAction = function ScanAction(_ref2) {
  var isNativeApp = _ref2.isNativeApp,
    onClose = _ref2.onClose,
    onPayClick = _ref2.onPayClick,
    onScanCallback = _ref2.onScanCallback;
  var _useScannerDevice = useScannerDevice(),
    scannerStatus = _useScannerDevice.scannerStatus;
  var _useCameraDevice = useCameraDevice(),
    cameraStatus = _useCameraDevice.cameraStatus,
    activeCamera = _useCameraDevice.activeCamera;
  var _useScanListener = useScanListener(),
    scanResult = _useScanListener.scanResult;
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    inputValue = _useState2[0],
    setInputValue = _useState2[1];
  useEffect(function () {
    if (!!(scanResult !== null && scanResult !== void 0 && scanResult.value)) {
      onScanCallback(scanResult.value);
    }
  }, [scanResult]);
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan"
  }, !isNativeApp ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-header"
  }, /*#__PURE__*/React.createElement("span", {
    className: "pisell-eftpos-pay-huifu-scan-header-title"
  }, locales.getText('page.eftpos-pay.text.huifu.scan.title')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-header-close",
    onClick: onClose
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-x-close",
    style: {
      fontSize: '24px'
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-header-divider"
  })) : null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-content",
    style: {
      paddingTop: isNativeApp ? '0' : '24px'
    }
  }, !!isNativeApp && /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-content-title"
  }, locales.getText('page.eftpos-pay.text.huifu.scan.title')), /*#__PURE__*/React.createElement("div", {
    className: "".concat(DEFAULT_SCANNER_CLASS, " ").concat(DEFAULT_SCANNER_CLASS, "-").concat(scannerStatus ? 'active' : 'unactive')
  }, /*#__PURE__*/React.createElement(NativeDeviceStatus, {
    type: "scanner",
    status: scannerStatus
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-content-scanner-right"
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(DEFAULT_SCANNER_CLASS, "-").concat(scannerStatus ? 'active' : 'unactive', "-title")
  }, scannerStatus ? locales.getText('page.eftpos-pay.text.huifu.scan.gun.connected') : locales.getText('page.eftpos-pay.text.huifu.scan.gun.not-connected')), /*#__PURE__*/React.createElement("span", {
    className: "".concat(DEFAULT_SCANNER_CLASS, "-").concat(scannerStatus ? 'active' : 'unactive', "-desc")
  }, scannerStatus ? locales.getText('page.eftpos-pay.text.huifu.scan.gun.connected-desc') : locales.getText('page.eftpos-pay.text.huifu.scan.gun.not-connected-desc')))), cameraStatus && /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-content-camera",
    onClick: activeCamera
  }, /*#__PURE__*/React.createElement(NativeDeviceStatus, {
    type: "camera",
    status: cameraStatus
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-content-camera-right"
  }, /*#__PURE__*/React.createElement("span", {
    className: "pisell-eftpos-pay-huifu-scan-content-camera-title"
  }, locales.getText('page.eftpos-pay.text.huifu.scan.camera-title')), /*#__PURE__*/React.createElement("span", {
    className: "pisell-eftpos-pay-huifu-scan-content-camera-desc"
  }, locales.getText('page.eftpos-pay.text.huifu.scan.camera-desc')))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-content-input-wrapper"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-content-input-wrapper-title"
  }, locales.getText('page.eftpos-pay.text.huifu.scan.input-title')), /*#__PURE__*/React.createElement(Input, {
    placeholder: locales.getText('page.eftpos-pay.text.huifu.scan.input-placeholder'),
    maxLength: 18,
    style: {
      width: '100%',
      fontSize: '16px'
    },
    size: "large",
    value: inputValue,
    onChange: function onChange(e) {
      var _e$target;
      // 使用正则控制只能输入0-9字符，且长度不超过18
      var value = (e === null || e === void 0 || (_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.value) || '';
      var regex = /^[0-9]*$/;
      if (regex.test(value) && (value === null || value === void 0 ? void 0 : value.length) <= 18) {
        setInputValue(value);
      }
    }
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-content-input-wrapper-tip"
  }, "\uD83D\uDCA1", locales.getText('page.eftpos-pay.text.huifu.scan.input-tip')))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-footer"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-huifu-scan-footer-divider"
  }), /*#__PURE__*/React.createElement(Button, {
    type: "default",
    block: true,
    size: "large",
    className: "pisell-eftpos-pay-huifu-scan-footer-button",
    onClick: onClose
  }, locales.getText('page.eftpos-pay.text.huifu.scan.cancel')), /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    block: true,
    size: "large",
    disabled: !inputValue || (inputValue === null || inputValue === void 0 ? void 0 : inputValue.length) < 18,
    onClick: function onClick() {
      return onPayClick(inputValue);
    },
    className: "pisell-eftpos-pay-huifu-scan-footer-button"
  }, locales.getText('page.eftpos-pay.text.huifu.scan.confirm'))));
};