"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AuthsignalPasskey = void 0;
var _reactNative = require("react-native");
var _error = require("./error");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
let initialized = false;
let autofillRequestPending = false;
const AuthsignalPasskeyModule = _reactNative.NativeModules.AuthsignalPasskeyModule ? _reactNative.NativeModules.AuthsignalPasskeyModule : new Proxy({}, {
  get() {
    throw new Error(_error.LINKING_ERROR);
  }
});
class AuthsignalPasskey {
  constructor(_ref) {
    let {
      tenantID,
      baseURL,
      deviceID,
      enableLogging
    } = _ref;
    _defineProperty(this, "tenantID", void 0);
    _defineProperty(this, "baseURL", void 0);
    _defineProperty(this, "deviceID", void 0);
    _defineProperty(this, "enableLogging", void 0);
    this.tenantID = tenantID;
    this.baseURL = baseURL;
    this.deviceID = deviceID;
    this.enableLogging = enableLogging;
  }
  async signUp() {
    let {
      token,
      username,
      displayName
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    await this.ensureModuleIsInitialized();
    try {
      const data = await AuthsignalPasskeyModule.signUp(token, username, displayName);
      return {
        data
      };
    } catch (ex) {
      if (this.enableLogging) {
        console.log(ex);
      }
      return (0, _error.handleErrorCodes)(ex);
    }
  }
  async signIn() {
    let {
      action,
      token,
      autofill = false,
      preferImmediatelyAvailableCredentials = true
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    await this.ensureModuleIsInitialized();
    try {
      if (autofill) {
        if (autofillRequestPending) {
          return {};
        } else {
          autofillRequestPending = true;
        }
      }
      if (_reactNative.Platform.OS === 'ios') {
        const data = await AuthsignalPasskeyModule.signIn(action, token, autofill, preferImmediatelyAvailableCredentials);
        autofillRequestPending = false;
        return {
          data
        };
      } else {
        const data = await AuthsignalPasskeyModule.signIn(action, token, preferImmediatelyAvailableCredentials);
        autofillRequestPending = false;
        return {
          data
        };
      }
    } catch (ex) {
      if (this.enableLogging && !autofill) {
        console.log(ex);
      }
      autofillRequestPending = false;
      return (0, _error.handleErrorCodes)(ex);
    }
  }
  cancel() {
    if (_reactNative.Platform.OS === 'ios') {
      AuthsignalPasskeyModule.cancel();
    }
  }
  isSupported() {
    if (_reactNative.Platform.OS === 'android') {
      return _reactNative.Platform.Version >= 28;
    } else if (_reactNative.Platform.OS === 'ios') {
      return parseInt(_reactNative.Platform.Version, 10) >= 15;
    } else {
      return false;
    }
  }

  /**
   * @deprecated Use 'preferImmediatelyAvailableCredentials' to control what happens when a passkey isn't available.
   */
  async isAvailableOnDevice() {
    await this.ensureModuleIsInitialized();
    return await AuthsignalPasskeyModule.isAvailableOnDevice();
  }
  async ensureModuleIsInitialized() {
    if (initialized) {
      return;
    }
    await AuthsignalPasskeyModule.initialize(this.tenantID, this.baseURL, this.deviceID);
    initialized = true;
  }
}
exports.AuthsignalPasskey = AuthsignalPasskey;
//# sourceMappingURL=passkey.js.map