var _excluded = ["style", "text", "children"];
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef, useRef, useImperativeHandle, useEffect } from 'react';
import extractNumber from "./extractNumber";
import Instance from "./instance.js";

// 生成二维码
var QRCode = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var style = _ref.style,
    text = _ref.text,
    children = _ref.children,
    props = _objectWithoutProperties(_ref, _excluded);
  // 节点
  var rootRef = useRef(null);
  var instance = useRef(null);
  useImperativeHandle(ref, function () {
    return {
      rootDOM: rootRef.current,
      instance: instance.current,
      getRootDOM: function getRootDOM() {
        return rootRef.current;
      },
      getInstance: function getInstance() {
        return instance.current;
      }
    };
  });
  useEffect(function () {
    if (!text || typeof text !== 'string') return;
    if (!instance.current) {
      instance.current = new Instance(rootRef.current, {
        text: text || '',
        width: extractNumber((style === null || style === void 0 ? void 0 : style.width) || 230),
        height: extractNumber((style === null || style === void 0 ? void 0 : style.width) || 230),
        colorDark: (style === null || style === void 0 ? void 0 : style.color) || '#000000',
        colorLight: (style === null || style === void 0 ? void 0 : style.backgroundColor) || '#ffffff',
        correctLevel: Instance.CorrectLevel.M // L,M,Q,H
      });
    }
    var width = extractNumber((style === null || style === void 0 ? void 0 : style.width) || 0);
    var height = extractNumber((style === null || style === void 0 ? void 0 : style.height) || 0);
    var color = style === null || style === void 0 ? void 0 : style.color;
    var backgroundColor = style === null || style === void 0 ? void 0 : style.backgroundColor;
    if (width) instance.current._htOption.width = width;
    if (height) instance.current._htOption.height = height;
    if (color) instance.current._htOption.colorDark = color;
    if (backgroundColor) instance.current._htOption.colorLight = backgroundColor;
    instance.current.makeCode(text);
    // eslint-disable-next-line
  }, [text]);
  if (!text) return null;
  return /*#__PURE__*/React.createElement("span", _extends({
    style: style
  }, props, {
    className: "qrcode".concat(props.className ? ' ' + props.className : ''),
    ref: rootRef
  }), children);
});
export default QRCode;