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 _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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 React, { useState, useMemo, useEffect } from 'react';
import { VirtualKeyboard } from '@pisell/materials';
import { Popover } from 'antd';
import RightPanel from "./RightPanel";
import CustomerCard from "./CustomerCard";
import "./index.less";
import { locales } from '@pisell/utils';
import useEngineContext from "../../hooks/useEngineContext";
import localeTexts from "./locales";
import { getLocale } from "../../utils/locales";
var items = [{
  value: 1,
  label: '1',
  disabled: false,
  type: 'text'
}, {
  value: 2,
  label: '2',
  disabled: false,
  type: 'text'
}, {
  value: 3,
  label: '3',
  disabled: false,
  type: 'text'
}, {
  value: 4,
  label: '4',
  disabled: false,
  type: 'text'
}, {
  value: 5,
  label: '5',
  disabled: false,
  type: 'text'
}, {
  value: 6,
  label: '6',
  disabled: false,
  type: 'text'
}, {
  value: 7,
  label: '7',
  disabled: false,
  type: 'text'
}, {
  value: 8,
  label: '8',
  disabled: false,
  type: 'text'
}, {
  value: 9,
  label: '9',
  disabled: false,
  type: 'text'
}, {
  value: '+',
  label: '+',
  disabled: false,
  type: 'text'
}, {
  value: '0',
  label: '0',
  disabled: false,
  type: 'text'
}, {
  value: 'reset',
  label: 'Reset',
  disabled: false,
  type: 'text'
}];
var PisellSelectCustomerModal = function PisellSelectCustomerModal(props) {
  var onChange = props.onChange,
    value = props.value,
    popoverProps = props.popoverProps,
    virtualKeyboardProps = props.virtualKeyboardProps,
    keyboardProps = props.keyboardProps,
    virtualKeyInputProps = props.virtualKeyInputProps,
    cardProps = props.cardProps;

  // 是否打开弹层
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  // 当前选择客户 -内部数据
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    _value = _useState4[0],
    set_value = _useState4[1];
  // 当前输入的值
  var _useState5 = useState(''),
    _useState6 = _slicedToArray(_useState5, 2),
    phone = _useState6[0],
    setPhone = _useState6[1];
  var context = useEngineContext();
  // 初始化多语言
  locales.init(localeTexts, getLocale());
  useEffect(function () {
    set_value(value);
  }, [value]);

  /**
   * @title: 返回逻辑
   * @description:
   * @param {any} e
   * @return {*}
   * @Author: zhiwei.Wang
   * @Date: 2024-11-02 09:30
   */
  var _onChange = function _onChange(e) {
    set_value(e);
    // setPhone('');
    onChange === null || onChange === void 0 || onChange(e);
    setOpen(false);
  };
  var onOpenChange = function onOpenChange(e) {
    setOpen(e);
  };

  // 防止页面过小时出现滚动条 造成页面滚动
  useEffect(function () {
    if (open) {
      document.body.classList.add('hide-scroll');
    } else {
      setTimeout(function () {
        document.body.classList.remove('hide-scroll');
      }, 150);
    }
  }, [open]);
  useEffect(function () {
    document.body.id = 'body';
  }, []);

  // 动态计算input的字体大小
  var adjustFontSize = useMemo(function () {
    // 没有内容固定位30px,  因为placeholder的字体大小是30px 太大会展示不全
    if (!phone) {
      return 30;
    }
    var maxFontSize = 60; // 最大字体大小
    var minFontSize = 10; // 最小字体大小
    var defaultFontSize = maxFontSize;

    // 获取输入框的宽度
    var inputWidth = 248;

    // 创建一个临时span元素用于测量内容宽度
    var span = document.createElement('span');
    span.style.position = 'absolute';
    span.style.whiteSpace = 'nowrap';
    span.style.visibility = 'hidden';
    span.style.fontSize = "".concat(defaultFontSize, "px");
    span.textContent = phone;
    document.body.appendChild(span);
    var fontSize = maxFontSize;

    // 动态调整字体大小直到内容宽度小于或等于输入框宽度
    while (span.offsetWidth > inputWidth && fontSize > minFontSize) {
      fontSize -= 0.5;
      span.style.fontSize = "".concat(fontSize, "px");
    }

    // 移除临时span元素
    document.body.removeChild(span);
    return fontSize;
  }, [phone]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Popover, _extends({
    content: /*#__PURE__*/React.createElement(VirtualKeyboard, _extends({
      style: {
        zoom: 0.8
      },
      keyboardProps: _objectSpread({
        keydown: false,
        items: items,
        onChange: function onChange(e) {
          if (e === 'reset') {
            setPhone('');
            return;
          }
          setPhone(phone + String(e));
        }
      }, keyboardProps),
      virtualKeyInputProps: _objectSpread({
        value: phone,
        onChange: function onChange(e) {
          setPhone(e.target.value);
        },
        placeholder: locales.getText('pisell2.text.search-customer-by-phone-number'),
        style: {
          textAlign: 'left',
          fontSize: adjustFontSize
        },
        className: 'pisell-select-customer-modal-input'
      }, virtualKeyInputProps),
      rightPanel: /*#__PURE__*/React.createElement(RightPanel, {
        value: _value,
        phone: phone,
        onChange: _onChange
      })
    }, virtualKeyboardProps)),
    open: open,
    placement: "bottom",
    arrow: false,
    onOpenChange: onOpenChange,
    trigger: ['click']
  }, popoverProps), /*#__PURE__*/React.createElement(CustomerCard, _extends({
    onClickItem: function onClickItem() {
      setOpen(true);
    },
    customer: _value,
    onRemove: _onChange
  }, cardProps))));
};
export default PisellSelectCustomerModal;