"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.auth = auth;
exports.createRecoveryFile = createRecoveryFile;
exports.decryptRecoveryFile = decryptRecoveryFile;
exports.generateSecretKey = generateSecretKey;
exports.get = get;
exports.getPublicKeyFromSecretKey = getPublicKeyFromSecretKey;
exports.list = list;
exports.parseAuthUrl = parseAuthUrl;
exports.publish = publish;
exports.publishHttps = publishHttps;
exports.put = put;
exports.resolve = resolve;
exports.resolveHttps = resolveHttps;
exports.signIn = signIn;
exports.signOut = signOut;
exports.signUp = signUp;
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);
  }
});
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));
  }
}
async function signUp(secretKey, homeserver) {
  try {
    const res = await Pubky.signUp(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)(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 (0, _result.ok)(JSON.parse(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 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));
  }
}
//# sourceMappingURL=index.js.map