"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

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

var _defaultTo = _interopRequireDefault(require("lodash/defaultTo"));

var _jsbarcode = _interopRequireDefault(require("jsbarcode"));

var BarCode = function BarCode(props) {
  var value = props.value,
      _props$renderAs = props.renderAs,
      renderAs = _props$renderAs === void 0 ? 'canvas' : _props$renderAs,
      bgColor = props.bgColor,
      fgColor = props.fgColor,
      option = props.option;
  var canvasBarCodeRef = (0, _react.useRef)(null);
  var svgBarCodeRef = (0, _react.useRef)(null);
  var renderCode = (0, _react.useCallback)(function () {
    try {
      var canvasRef = canvasBarCodeRef.current;
      var svgRef = svgBarCodeRef.current;
      if (canvasRef || svgRef) (0, _jsbarcode["default"])(renderAs === 'canvas' ? canvasRef : svgRef, value, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, option), {}, {
        background: (0, _defaultTo["default"])(option && option.background, bgColor),
        lineColor: (0, _defaultTo["default"])(option && option.lineColor, fgColor)
      }));
    } catch (error) {
      console.warn(error);
    }
  }, [canvasBarCodeRef, svgBarCodeRef]);
  (0, _react.useEffect)(function () {
    renderCode();
  }, [renderCode()]);

  if (renderAs === 'canvas') {
    return /*#__PURE__*/_react["default"].createElement("canvas", {
      ref: canvasBarCodeRef
    });
  }

  return /*#__PURE__*/_react["default"].createElement("svg", {
    ref: svgBarCodeRef
  });
};

var _default = BarCode;
exports["default"] = _default;
//# sourceMappingURL=BarCode.js.map
