'use strict';
'use warning';

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

var _typeof = require("@babel/runtime/helpers/typeof");

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _indexModule = _interopRequireWildcard(require("./../../../index.module.d"));

var _escpos = _interopRequireDefault(require("escpos"));

var _escposUsb = _interopRequireDefault(require("escpos-usb"));

var _escposNetwork = _interopRequireDefault(require("escpos-network"));

var _escposSerialport = _interopRequireDefault(require("escpos-serialport"));

var _lodash = _interopRequireDefault(require("lodash"));

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; }

_escpos["default"].USB = _escposUsb["default"];
_escpos["default"].network = _escposNetwork["default"];
_escpos["default"].serial = _escposSerialport["default"];

var Escpos = /*#__PURE__*/function () {
  /**
   *
   * @param {{}} config
   * @param {Number} config.type
   * @param {{}} config.settings
   * @param {String} config.settings.usb.vendorId
   * @param {String} config.settings.usb.productId
   * @param {type[]} config.settings.network.ipAddress
   * @param {type[]} config.settings.network.port
   * @param {type[]} config.settings.serial.port
   * @param {{}} config.settings.serial.settings
   * @param {Number} config.settings.serial.settings.baudRate
   * @param {Number} config.settings.serial.settings.stopBit
   * @return Promise<*>
   */
  function Escpos(config) {
    var _this = this;

    (0, _classCallCheck2["default"])(this, Escpos);
    (0, _defineProperty2["default"])(this, "config", null);
    (0, _defineProperty2["default"])(this, "device", null);
    (0, _defineProperty2["default"])(this, "print", /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
        var printerOptions,
            _args3 = arguments;
        return _regenerator["default"].wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                printerOptions = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {};
                return _context3.abrupt("return", new Promise( /*#__PURE__*/function () {
                  var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(resolve, rejected) {
                    return _regenerator["default"].wrap(function _callee2$(_context2) {
                      while (1) {
                        switch (_context2.prev = _context2.next) {
                          case 0:
                            _this.device.open( /*#__PURE__*/function () {
                              var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(error) {
                                return _regenerator["default"].wrap(function _callee$(_context) {
                                  while (1) {
                                    switch (_context.prev = _context.next) {
                                      case 0:
                                        !error ? resolve(_this.printer) : rejected({
                                          status: false,
                                          code: 501,
                                          msg: "Cannot Open The Printer",
                                          error: error
                                        });

                                      case 1:
                                      case "end":
                                        return _context.stop();
                                    }
                                  }
                                }, _callee);
                              }));

                              return function (_x3) {
                                return _ref3.apply(this, arguments);
                              };
                            }());

                          case 1:
                          case "end":
                            return _context2.stop();
                        }
                      }
                    }, _callee2);
                  }));

                  return function (_x, _x2) {
                    return _ref2.apply(this, arguments);
                  };
                }()));

              case 2:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      }));

      return function () {
        return _ref.apply(this, arguments);
      };
    }());
    this.config = _lodash["default"].extend(_indexModule["default"].config.Hardware.Printer.Escpos, config);
    return new Promise( /*#__PURE__*/function () {
      var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(resolve, rejected) {
        return _regenerator["default"].wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.t0 = _this.config.type;
                _context4.next = _context4.t0 === _indexModule.Options.ESCPOS_TYPE_USB ? 3 : _context4.t0 === _indexModule.Options.ESCPOS_TYPE_NETWORK ? 17 : _context4.t0 === _indexModule.Options.ESCPOS_TYPE_SERIAL ? 24 : 38;
                break;

              case 3:
                if (!(_this.config.settings.vendorId !== undefined && _this.config.settings.usb.productId !== undefined)) {
                  _context4.next = 9;
                  break;
                }

                _context4.next = 6;
                return new _escpos["default"].USB(_this.config.settings.usb.vendorId, _this.config.settings.usb.productId);

              case 6:
                _context4.t1 = _context4.sent;
                _context4.next = 12;
                break;

              case 9:
                _context4.next = 11;
                return new _escpos["default"].USB();

              case 11:
                _context4.t1 = _context4.sent;

              case 12:
                _this.device = _context4.t1;
                _context4.next = 15;
                return new _escpos["default"].Printer(_this.device, _this.config.options);

              case 15:
                _this.printer = _context4.sent;
                return _context4.abrupt("break", 38);

              case 17:
                _context4.next = 19;
                return new _escpos["default"].network(_this.config.settings.network.ipAddress, _this.config.settings.network.port);

              case 19:
                _this.device = _context4.sent;
                _context4.next = 22;
                return new _escpos["default"].Printer(_this.device, _this.config.options);

              case 22:
                _this.printer = _context4.sent;
                return _context4.abrupt("break", 38);

              case 24:
                if (!(_this.config.settings.serial.settings !== undefined)) {
                  _context4.next = 30;
                  break;
                }

                _context4.next = 27;
                return new _escpos["default"].serial(_this.config.settings.serial.port, _this.config.settings.serial.settings);

              case 27:
                _context4.t2 = _context4.sent;
                _context4.next = 33;
                break;

              case 30:
                _context4.next = 32;
                return new _escpos["default"].serial(_this.config.settings.serial.port);

              case 32:
                _context4.t2 = _context4.sent;

              case 33:
                _this.device = _context4.t2;
                _context4.next = 36;
                return new _escpos["default"].Printer(_this.device, _this.config.options);

              case 36:
                _this.printer = _context4.sent;
                return _context4.abrupt("break", 38);

              case 38:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4);
      }));

      return function (_x4, _x5) {
        return _ref4.apply(this, arguments);
      };
    }());
  }

  (0, _createClass2["default"])(Escpos, null, [{
    key: "ESCPOS_TYPE_USB",
    get: function get() {
      return _indexModule.Options.ESCPOS_TYPE_USB;
    }
  }, {
    key: "ESCPOS_TYPE_NETWORK",
    get: function get() {
      return _indexModule.Options.ESCPOS_TYPE_NETWORK;
    }
  }, {
    key: "ESCPOS_TYPE_SERIAL",
    get: function get() {
      return _indexModule.Options.ESCPOS_TYPE_SERIAL;
    }
  }]);
  return Escpos;
}();

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