import { NativeModules, NativeEventEmitter, Platform } from 'react-native';
import { getPrinterSeriesByName, requestAndroidPermissions, enableLocationAccessAndroid10, getPrinterLanguage } from './utils';
import { PRINTER_SERIES, FONT_A_CHARS_PER_LINE, DEFAULT_FONT_A_CHARS_PER_LINE, DEFAULT_PAPER_WIDTH, PRINTER_LANGUAGE } from './constants';
const {
  EscPosPrinter,
  EscPosPrinterDiscovery,
  ThePrinterWrapper
} = NativeModules;
const discoveryEventEmmiter = new NativeEventEmitter(EscPosPrinterDiscovery);
const printEventEmmiter = new NativeEventEmitter(EscPosPrinter);
import printing from './printing';
const _default = {
  init(_ref) {
    let {
      target,
      seriesName,
      language = 'EPOS2_LANG_EN'
    } = _ref;
    const series = PRINTER_SERIES[seriesName];
    const lang = getPrinterLanguage(language);
    return EscPosPrinter.init(target, series, lang);
  },

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

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

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

  async discover(params) {
    if (Platform.OS === 'ios' || (await requestAndroidPermissions()) && (await 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()) || DEFAULT_PAPER_WIDTH;
    const key = String(paperWidth);
    const seriesCharsPerLineFontA = FONT_A_CHARS_PER_LINE[seriesName];
    const fontAcplForCurrentWidth = seriesCharsPerLineFontA === null || seriesCharsPerLineFontA === void 0 ? void 0 : seriesCharsPerLineFontA[key];
    return {
      fontA: fontAcplForCurrentWidth || DEFAULT_FONT_A_CHARS_PER_LINE[paperWidth]
    };
  },

  pairingBluetoothPrinter() {
    if (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
};
export { getPrinterSeriesByName, PRINTER_SERIES, PRINTER_LANGUAGE };
export default _default;
//# sourceMappingURL=index.js.map