"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.open = open;
var _reactNative = require("react-native");
const {
  RNFileViewerChatVT
} = _reactNative.NativeModules;
const eventEmitter = new _reactNative.NativeEventEmitter(RNFileViewerChatVT);
let lastId = 0;
function open(path) {
  let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  const _options = typeof options === "string" ? {
    displayName: options
  } : options;
  const {
    onDismiss,
    ...nativeOptions
  } = _options;
  if (!["android", "ios"].includes(_reactNative.Platform.OS)) {
    return RNFileViewerChatVT.open(path, nativeOptions);
  }
  return new Promise((resolve, reject) => {
    const currentId = ++lastId;
    const openSubscription = eventEmitter.addListener("RNFileViewerDidOpen", _ref => {
      let {
        id,
        error
      } = _ref;
      if (id === currentId) {
        openSubscription.remove();
        return error ? reject(new Error(error)) : resolve();
      }
    });
    const dismissSubscription = eventEmitter.addListener("RNFileViewerDidDismiss", _ref2 => {
      let {
        id
      } = _ref2;
      if (id === currentId) {
        dismissSubscription.remove();
        onDismiss && onDismiss();
      }
    });
    RNFileViewerChatVT.open(normalize(path), currentId, nativeOptions);
  });
}
function normalize(path) {
  if (_reactNative.Platform.OS === "ios" || _reactNative.Platform.OS === "android") {
    const filePrefix = "file://";
    if (path.startsWith(filePrefix)) {
      path = path.substring(filePrefix.length);
      try {
        path = decodeURI(path);
      } catch (e) {}
    }
  }
  return path;
}
var _default = {
  open
};
exports.default = _default;
//# sourceMappingURL=index.js.map