"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.auth = auth;
exports.createRecoveryFile = createRecoveryFile;
exports.decryptRecoveryFile = decryptRecoveryFile;
exports.deleteFile = deleteFile;
exports.generateMnemonicPhrase = generateMnemonicPhrase;
exports.generateMnemonicPhraseAndKeypair = generateMnemonicPhraseAndKeypair;
exports.generateSecretKey = generateSecretKey;
exports.get = get;
exports.getHomeserver = getHomeserver;
exports.getPublicKeyFromSecretKey = getPublicKeyFromSecretKey;
exports.getSignupToken = getSignupToken;
exports.list = list;
exports.mnemonicPhraseToKeypair = mnemonicPhraseToKeypair;
exports.parseAuthUrl = parseAuthUrl;
exports.publish = publish;
exports.publishHttps = publishHttps;
exports.put = put;
exports.removeEventListener = removeEventListener;
exports.republishHomeserver = republishHomeserver;
exports.resolve = resolve;
exports.resolveHttps = resolveHttps;
exports.session = session;
exports.setEventListener = setEventListener;
exports.signIn = signIn;
exports.signOut = signOut;
exports.signUp = signUp;
exports.validateMnemonicPhrase = validateMnemonicPhrase;
var _reactNative = require("react-native");
var _result = require("@synonymdev/result");
const LINKING_ERROR = `The package 'react-native-pubky' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({
  ios: "- You have run 'pod install'\n",
  default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
const Pubky = _reactNative.NativeModules.Pubky ? _reactNative.NativeModules.Pubky : new Proxy({}, {
  get() {
    throw new Error(LINKING_ERROR);
  }
});
const eventEmitter = new _reactNative.NativeEventEmitter(Pubky);
async function setEventListener(callback) {
  try {
    await Pubky.setEventListener();
    eventEmitter.addListener('PubkyEvent', callback);
    return (0, _result.ok)(undefined);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function removeEventListener() {
  try {
    //await Pubky.removeEventListener();
    eventEmitter.removeAllListeners('PubkyEvent');
    return (0, _result.ok)(undefined);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function auth(url, secretKey) {
  const res = await Pubky.auth(url, secretKey);
  if (res[0] === 'error') {
    return (0, _result.err)(res[1]);
  }
  return (0, _result.ok)(res[1]);
}
async function parseAuthUrl(url) {
  try {
    const res = await Pubky.parseAuthUrl(url);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    const parsed = JSON.parse(res[1]);
    return (0, _result.ok)(parsed);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function publish(recordName, recordContent, secretKey) {
  try {
    const res = await Pubky.publish(recordName, recordContent, secretKey);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function resolve(publicKey) {
  try {
    const res = await Pubky.resolve(publicKey);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}

/*
Returns the signupToken used in signUp
 */
async function getSignupToken(homeserverPubky, adminPassword) {
  try {
    const res = await Pubky.getSignupToken(homeserverPubky, adminPassword);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function signUp(secretKey, homeserver, signupToken) {
  try {
    const res = await Pubky.signUp(secretKey, homeserver, signupToken);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function republishHomeserver(secretKey, homeserver) {
  try {
    const res = await Pubky.republishHomeserver(secretKey, homeserver);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function signIn(secretKey) {
  try {
    const res = await Pubky.signIn(secretKey);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function signOut(secretKey) {
  try {
    const res = await Pubky.signOut(secretKey);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function get(url) {
  try {
    const res = await Pubky.get(url);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    // Return the raw response directly
    // It will be either:
    // - Plain text (for UTF-8 content)
    // - "base64:..." (for binary content)
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function put(url, content) {
  try {
    const res = await Pubky.put(url, JSON.stringify(content));
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function publishHttps(recordName, target, secretKey) {
  try {
    const res = await Pubky.publishHttps(recordName, target, secretKey);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function resolveHttps(publicKey) {
  try {
    const res = await Pubky.resolveHttps(publicKey);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function list(url) {
  try {
    const res = await Pubky.list(url);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function deleteFile(url) {
  try {
    const res = await Pubky.deleteFile(url);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function session(pubky) {
  try {
    const res = await Pubky.session(pubky);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function generateSecretKey() {
  try {
    const res = await Pubky.generateSecretKey();
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function getPublicKeyFromSecretKey(secretKey) {
  try {
    const res = await Pubky.getPublicKeyFromSecretKey(secretKey);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function createRecoveryFile(secretKey, passphrase) {
  try {
    const res = await Pubky.createRecoveryFile(secretKey, passphrase);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function decryptRecoveryFile(recoveryFile, passphrase) {
  try {
    const res = await Pubky.decryptRecoveryFile(recoveryFile, passphrase);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function getHomeserver(pubky) {
  try {
    const res = await Pubky.getHomeserver(pubky);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function generateMnemonicPhrase() {
  try {
    const res = await Pubky.generateMnemonicPhrase();
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1]);
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function mnemonicPhraseToKeypair(mnemonicPhrase) {
  try {
    const res = await Pubky.mnemonicPhraseToKeypair(mnemonicPhrase);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function generateMnemonicPhraseAndKeypair() {
  try {
    const res = await Pubky.generateMnemonicPhraseAndKeypair();
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(JSON.parse(res[1]));
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
async function validateMnemonicPhrase(mnemonicPhrase) {
  try {
    const res = await Pubky.validateMnemonicPhrase(mnemonicPhrase);
    if (res[0] === 'error') {
      return (0, _result.err)(res[1]);
    }
    return (0, _result.ok)(res[1] === 'true');
  } catch (e) {
    return (0, _result.err)(JSON.stringify(e));
  }
}
//# sourceMappingURL=index.js.map