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 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 _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 { locales } from '@pisell/utils';
import { useMount } from 'ahooks';
import { Spin, message } from 'antd';
import React, { useState } from 'react';
import Titlebar from "./component/header/titlebar";
import { ModeEnum } from "./const";
import { getPayAmonunt } from "./helper";
import ChevronRight from '@pisell/icon/es/ChevronRight';
import { useDispatch, useContextData, updateDevice, updateState } from "./store";
import { getAmonunt } from "./manufacturer";
import "./device.less";

/**
 * @title: 设备选择页
 * @description: 选择设备信息,无设备增加提示;仅有一条,默认选中跳走;多条记录则停留等待用户选择
 * @return React.Element
 * @Author: hongbing.wang
 * @Date: 2024-08-12 11:40
 */
export default (function (_ref) {
  var api = _ref.api,
    onChange = _ref.onChange,
    onClose = _ref.onClose,
    _ref$className = _ref.className,
    className = _ref$className === void 0 ? '' : _ref$className,
    device_number = _ref.device_number,
    isMobile = _ref.isMobile,
    net = _ref.net,
    client = _ref.client,
    _ref$formatAmount = _ref.formatAmount,
    formatAmount = _ref$formatAmount === void 0 ? function (number, precision, symbol) {
      return "".concat(symbol).concat(number);
    } : _ref$formatAmount,
    _ref$containerStyle = _ref.containerStyle,
    containerStyle = _ref$containerStyle === void 0 ? {} : _ref$containerStyle;
  var store = useContextData();
  var device = store.device,
    symbol = store.symbol,
    mode = store.mode,
    eftpos = store.eftpos;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    loading = _useState2[0],
    setLoading = _useState2[1];
  var _useState3 = useState(true),
    _useState4 = _slicedToArray(_useState3, 2),
    dataStaus = _useState4[0],
    setDataStaus = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    list = _useState6[0],
    setList = _useState6[1];
  var dispatch = useDispatch();

  /**
   * @title: 设备选择事件
   * @param {*} event
   * @Author: hongbing.wang
   * @Date: 2024-08-20 13:02
   */
  var onClick = function onClick(event) {
    if (!net) {
      message.open({
        key: 'net-warning',
        type: 'warning',
        content: locales.getText('page.eftpos-pay.text.not.connected')
      });
      return;
    }
    var _ref2 = [Number(event.currentTarget.dataset.index)],
      index = _ref2[0];
    setDevice(list[index]);
  };

  /**
   * @title: 初始化加载设备列表页
   * @description: 初始化加载设备列表页 若只有一条数据 则直接进入下一步
   * @Author: hongbing.wang
   * @Date: 2024-08-20 13:02
   */
  useMount(function () {
    setLoading(true);
    api.getDeviceList({
      type: eftpos,
      status: 'paired'
    }).then(function (res) {
      setLoading(false);
      if (res && Array.isArray(res.list) && res.list.length > 0) {
        if (device_number) {
          var hostDeviceList = res.list.filter(function (item) {
            var _item$setting;
            return item === null || item === void 0 || (_item$setting = item.setting) === null || _item$setting === void 0 || (_item$setting = _item$setting.devices) === null || _item$setting === void 0 ? void 0 : _item$setting.some(function (i) {
              return i.number === device_number;
            });
          });
          if (hostDeviceList.length >= 1) {
            setDataStaus(true);
            setList(setDeviceList(hostDeviceList));
            if (hostDeviceList.length === 1) {
              setDevice(hostDeviceList[0]);
            }
            return;
          }
        }
        setDataStaus(true);
        setList(setDeviceList(res.list));
        if (res.list.length === 1) {
          setDevice(res.list[0]);
        }
        return;
      }
      setDataStaus(false);
    }, function () {
      setDataStaus(false);
      setLoading(false);
    });
  });
  var setDevice = function setDevice(device) {
    dispatch(updateDevice({
      device: device
    }));
    if (typeof api.selectDevice === 'function') {
      api.selectDevice(device);
    }
    if (typeof onChange === 'function') {
      if (mode === ModeEnum.Pay) {
        if (device.pay) {
          dispatch(updateState({
            pay: device.pay
          }));
        } else {
          var amonunt = getAmonunt(_objectSpread(_objectSpread({}, store), {}, {
            device: device
          }));
          var pay = getPayAmonunt(amonunt, function (total) {
            return formatAmount(total, 2, symbol);
          });
          dispatch(updateState({
            pay: pay
          }));
        }
      }
      onChange('page', mode === ModeEnum.FullPay ? 'amount' : 'pay');
    }
  };
  var setDeviceList = function setDeviceList(list) {
    return list.map(function (item) {
      var amonunt = getAmonunt(_objectSpread(_objectSpread({}, store), {}, {
        device: item
      }));
      var pay = getPayAmonunt(amonunt, function (total) {
        return formatAmount(total, 2, symbol);
      });
      return _objectSpread(_objectSpread({}, item), {}, {
        pay: pay
      });
    });
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-device-box ".concat(className)
  }, isMobile ? null : /*#__PURE__*/React.createElement(Titlebar, {
    type: "left-title-close",
    onClose: onClose
  }, dataStaus ? locales.getText('page.eftpos-pay.text.device') : ''), dataStaus ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-device-list",
    style: _objectSpread(_objectSpread({}, isMobile ? {
      marginTop: '20px'
    } : {}), containerStyle || {})
  }, /*#__PURE__*/React.createElement(Spin, {
    spinning: loading,
    style: {
      flex: 1,
      overflow: 'auto'
    }
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex',
      flexDirection: 'column',
      gap: '10px'
    }
  }, list.map(function (_ref3, index) {
    var id = _ref3.id,
      name = _ref3.name,
      pay = _ref3.pay;
    return /*#__PURE__*/React.createElement("div", {
      key: id,
      className: "pisell-lowcode__eft-pay-device ".concat(id === (device === null || device === void 0 ? void 0 : device.id) ? 'active' : ''),
      "data-index": index,
      onClick: onClick,
      role: "button",
      tabIndex: index
    }, /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__eft-pay-device-content"
    }, /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__eft-pay-device-name"
    }, name), mode === ModeEnum.Refund ? null : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__eft-pay-device-money"
    }, pay.totalUnit), /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__eft-pay-device-sucharge"
    }, locales.getText('page.eftpos-pay.text.surcharge.edit.inc.surcharge'), ": ", formatAmount("".concat(pay.surcharge), 2, symbol), " (", locales.getText('page.eftpos-pay.text.surcharge.edit.rate'), ":", pay.surRate, "% +", ' ', formatAmount("".concat(pay.surMoney), 2, symbol), ")"))), /*#__PURE__*/React.createElement("div", {
      style: {
        flexShrink: 0
      }
    }, /*#__PURE__*/React.createElement(ChevronRight, {
      style: {
        fontWeight: '600',
        fontSize: '24px'
      }
    })));
  })))) : /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-device-error",
    style: isMobile ? {
      marginTop: '20px'
    } : {}
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-device-error-tit"
  }, locales.getText('page.eftpos-pay.text.device.no.title')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-device-error-txt"
  }, locales.getText('page.eftpos-pay.text.device.no.add')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-device-error-txt"
  }, locales.getText('page.eftpos-pay.text.device.no.web')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-device-error-txt"
  }, locales.getText('page.eftpos-pay.text.device.no.terminal'))));
});