"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _instance = _interopRequireDefault(require("./instance.js"));

var _excluded = ["text", "style", "children"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || 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 _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; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var QRCode = /*#__PURE__*/function (_Component) {
  _inheritsLoose(QRCode, _Component);

  function QRCode() {
    return _Component.apply(this, arguments) || this;
  }

  var _proto = QRCode.prototype;

  _proto.componentDidUpdate = function componentDidUpdate() {
    var _this$props = this.props,
        text = _this$props.text,
        _this$props$style = _this$props.style,
        style = _this$props$style === void 0 ? {} : _this$props$style;
    if (!this.instance || !text) return;
    var width = Object.getUnitNum(style.width || 0);
    var height = Object.getUnitNum(style.height || 0);
    var color = style.color;
    var backgroundColor = style.backgroundColor;
    if (width) this.instance._htOption.width = width;
    if (height) this.instance._htOption.height = height;
    if (color) this.instance._htOption.colorDark = color;
    if (backgroundColor) this.instance._htOption.colorLight = backgroundColor;
    this.instance.makeCode(text);
  };

  _proto.componentDidMount = function componentDidMount() {
    if (this.instance) return;
    var _this$props2 = this.props,
        text = _this$props2.text,
        _this$props2$style = _this$props2.style,
        style = _this$props2$style === void 0 ? {} : _this$props2$style;
    this.instance = new _instance["default"](this.$el, {
      text: text || '',
      width: Object.getUnitNum(style.width || 230),
      height: Object.getUnitNum(style.width || 230),
      colorDark: style.color || '#000000',
      colorLight: style.backgroundColor || '#ffffff',
      correctLevel: _instance["default"].CorrectLevel.M // L,M,Q,H

    });
  };

  _proto.render = function render() {
    var _this = this;

    var _this$props3 = this.props,
        text = _this$props3.text,
        style = _this$props3.style,
        children = _this$props3.children,
        others = _objectWithoutPropertiesLoose(_this$props3, _excluded);

    if (!text) return null;
    return /*#__PURE__*/_react["default"].createElement("span", _extends({
      ref: function ref(el) {
        _this.$el = el;
      },
      style: style
    }, others, {
      className: "qrcode" + (others.className ? ' ' + others.className : '')
    }), children);
  };

  return QRCode;
}(_react.Component);

exports["default"] = QRCode;

_defineProperty(QRCode, "defaultProps", {});

QRCode.propTypes = process.env.NODE_ENV !== "production" ? {
  text: _propTypes["default"].string,
  style: _propTypes["default"].object,
  children: _propTypes["default"].node
} : {};
module.exports = exports.default;