"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "PRINTER_LANGUAGE", {
  enumerable: true,
  get: function () {
    return _constants.PRINTER_LANGUAGE;
  }
});
Object.defineProperty(exports, "PRINTER_SERIES", {
  enumerable: true,
  get: function () {
    return _constants.PRINTER_SERIES;
  }
});
exports.default = void 0;
Object.defineProperty(exports, "getPrinterSeriesByName", {
  enumerable: true,
  get: function () {
    return _utils.getPrinterSeriesByName;
  }
});

var _reactNative = require("react-native");

var _utils = require("./utils");

var _constants = require("./constants");

var _printing = _interopRequireDefault(require("./printing"));

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

const {
  EscPosPrinter,
  EscPosPrinterDiscovery,
  ThePrinterWrapper
} = _reactNative.NativeModules;
const discoveryEventEmmiter = new _reactNative.NativeEventEmitter(EscPosPrinterDiscovery);
const printEventEmmiter = new _reactNative.NativeEventEmitter(EscPosPrinter);
const _default = {
  init(_ref) {
    let {
      target,
      seriesName,
      language = 'EPOS2_LANG_EN'
    } = _ref;
    const series = _constants.PRINTER_SERIES[seriesName];
    const lang = (0, _utils.getPrinterLanguage)(language);
    return EscPosPrinter.init(target, series, lang);
  },

  instantiate(_ref2) {
    let {
      target,
      seriesName,
      language = 'EPOS2_LANG_EN'
    } = _ref2;
    const series = _constants.PRINTER_SERIES[seriesName];
    const lang = (0, _utils.getPrinterLanguage)(language);
    return ThePrinterWrapper.init(target, series, lang);
  },

  connect(target) {
    return ThePrinterWrapper.connect(target);
  },

  disconnectPrinter(target) {
    return ThePrinterWrapper.disconnectAndDeallocate(target);
  },

  async discover(params) {
    if (_reactNative.Platform.OS === 'ios' || (await (0, _utils.requestAndroidPermissions)()) && (await (0, _utils.enableLocationAccessAndroid10)())) {
      return new Promise((res, rej) => {
        let listener;

        function removeListener() {
          var _listener;

          (_listener = listener) === null || _listener === void 0 ? void 0 : _listener.remove();
          listener = null;
        }

        listener = discoveryEventEmmiter.addListener('onDiscoveryDone', printers => {
          res(printers);
          removeListener();
        });
        EscPosPrinterDiscovery.discover(params).then(() => {
          removeListener();
          res([]);
        }).catch(e => {
          removeListener();
          rej(e);
        });
      });
    }

    return Promise.reject('No permissions granted');
  },

  getPaperWidth() {
    let successListener;
    let errorListener;

    function removeListeners() {
      var _successListener, _errorListener;

      (_successListener = successListener) === null || _successListener === void 0 ? void 0 : _successListener.remove();
      (_errorListener = errorListener) === null || _errorListener === void 0 ? void 0 : _errorListener.remove();
      successListener = null;
      errorListener = null;
    }

    return new Promise((res, rej) => {
      successListener = printEventEmmiter.addListener('onGetPaperWidthSuccess', status => {
        removeListeners();
        res(status);
      });
      errorListener = printEventEmmiter.addListener('onGetPaperWidthFailure', status => {
        removeListeners();
        rej(status);
      });
      EscPosPrinter.getPaperWidth().catch(e => {
        removeListeners();
        rej(e);
      });
    });
  },

  async getPrinterCharsPerLine(seriesName) {
    const paperWidth = (await this.getPaperWidth()) || _constants.DEFAULT_PAPER_WIDTH;
    const key = String(paperWidth);
    const seriesCharsPerLineFontA = _constants.FONT_A_CHARS_PER_LINE[seriesName];
    const fontAcplForCurrentWidth = seriesCharsPerLineFontA === null || seriesCharsPerLineFontA === void 0 ? void 0 : seriesCharsPerLineFontA[key];
    return {
      fontA: fontAcplForCurrentWidth || _constants.DEFAULT_FONT_A_CHARS_PER_LINE[paperWidth]
    };
  },

  pairingBluetoothPrinter() {
    if (_reactNative.Platform.OS === 'ios') {
      return EscPosPrinter.pairingBluetoothPrinter();
    }

    return Promise.resolve('Successs');
  },

  disconnect() {
    EscPosPrinter.disconnect();
  },

  startMonitorPrinter() {
    let interval = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 5;
    return EscPosPrinter.startMonitorPrinter(Math.max(5, Math.floor(interval)));
  },

  stopMonitorPrinter() {
    return EscPosPrinter.stopMonitorPrinter();
  },

  addPrinterStatusListener(listener) {
    const statusListener = printEventEmmiter.addListener('onMonitorStatusUpdate', listener);
    return () => {
      statusListener.remove();
    };
  },

  printing: _printing.default
};
var _default2 = _default;
exports.default = _default2;
//# sourceMappingURL=index.js.map