'use strict';
'use warning';

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

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 _Options = _interopRequireDefault(require("./../../../../Options"));

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"));

_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.vendorId
   * @param {String} config.settings.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 _callee2() {
        var printerOptions,
            _args2 = arguments;
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                printerOptions = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : {};
                return _context2.abrupt("return", new Promise( /*#__PURE__*/function () {
                  var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(resolve, rejected) {
                    return _regenerator["default"].wrap(function _callee$(_context) {
                      while (1) {
                        switch (_context.prev = _context.next) {
                          case 0:
                            resolve(_this.printer);

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

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

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

      return function () {
        return _ref.apply(this, arguments);
      };
    }());
    this.config = _lodash["default"].extend({
      type: _Options["default"].ESCPOS_TYPE_USB,
      settings: {
        vendorId: undefined,
        productId: undefined,
        network: {
          ipAddress: "localhost",
          port: 9100
        },
        serial: {
          port: "COM2",
          settings: undefined
        }
      },
      options: {
        encoding: "GB18030"
      }
    }, config);
    return new Promise( /*#__PURE__*/function () {
      var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(resolve, rejected) {
        return _regenerator["default"].wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                _context6.t0 = _this.config.type;
                _context6.next = _context6.t0 === _Options["default"].ESCPOS_TYPE_USB ? 3 : _context6.t0 === _Options["default"].ESCPOS_TYPE_NETWORK ? 18 : _context6.t0 === _Options["default"].ESCPOS_TYPE_SERIAL ? 26 : 38;
                break;

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

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

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

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

              case 11:
                _context6.t1 = _context6.sent;

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

              case 15:
                _this.printer = _context6.sent;

                _this.device.open( /*#__PURE__*/function () {
                  var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(error) {
                    return _regenerator["default"].wrap(function _callee3$(_context3) {
                      while (1) {
                        switch (_context3.prev = _context3.next) {
                          case 0:
                            !error ? resolve(_this.printer) : rejected({
                              status: false,
                              code: 501,
                              msg: "Cannot Open The Printer",
                              error: error
                            });

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

                  return function (_x5) {
                    return _ref4.apply(this, arguments);
                  };
                }());
                /** End Find The Printer Function */
                //##################################################################


                return _context6.abrupt("break", 38);

              case 18:
                _context6.next = 20;
                return new _escpos["default"].network(_this.config.settings.network.ipAddress, _this.config.settings.network.port);

              case 20:
                _this.device = _context6.sent;
                _context6.next = 23;
                return new _escpos["default"].Printer(_this.device, _this.config.options);

              case 23:
                _this.printer = _context6.sent;

                _this.device.open( /*#__PURE__*/function () {
                  var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(error) {
                    return _regenerator["default"].wrap(function _callee4$(_context4) {
                      while (1) {
                        switch (_context4.prev = _context4.next) {
                          case 0:
                            !error ? resolve(_this.printer) : rejected({
                              status: false,
                              code: 501,
                              msg: "Cannot Open The Printer",
                              error: error
                            });

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

                  return function (_x6) {
                    return _ref5.apply(this, arguments);
                  };
                }());

                return _context6.abrupt("break", 38);

              case 26:
                if (!(_this.config.settings.serial.settings !== undefined)) {
                  _context6.next = 32;
                  break;
                }

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

              case 29:
                _context6.t2 = _context6.sent;
                _context6.next = 35;
                break;

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

              case 34:
                _context6.t2 = _context6.sent;

              case 35:
                _this.device = _context6.t2;

                _this.device.open( /*#__PURE__*/function () {
                  var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(error) {
                    return _regenerator["default"].wrap(function _callee5$(_context5) {
                      while (1) {
                        switch (_context5.prev = _context5.next) {
                          case 0:
                            !error ? resolve(_this.printer) : rejected({
                              status: false,
                              code: 501,
                              msg: "Cannot Open The Printer",
                              error: error
                            });

                          case 1:
                          case "end":
                            return _context5.stop();
                        }
                      }
                    }, _callee5);
                  }));

                  return function (_x7) {
                    return _ref6.apply(this, arguments);
                  };
                }());

                return _context6.abrupt("break", 38);

              case 38:
              case "end":
                return _context6.stop();
            }
          }
        }, _callee6);
      }));

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

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

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