"use strict";

import { NativeModules, Platform, NativeEventEmitter } from 'react-native';
import { ok, err } from '@synonymdev/result';
const LINKING_ERROR = `The package 'react-native-pubky' doesn't seem to be linked. Make sure: \n\n` + 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 = NativeModules.Pubky ? NativeModules.Pubky : new Proxy({}, {
  get() {
    throw new Error(LINKING_ERROR);
  }
});
const eventEmitter = new NativeEventEmitter(Pubky);
export async function setEventListener(callback) {
  try {
    await Pubky.setEventListener();
    eventEmitter.addListener('PubkyEvent', callback);
    return ok(undefined);
  } catch (e) {
    return err(JSON.stringify(e));
  }
}
export async function removeEventListener() {
  try {
    //await Pubky.removeEventListener();
    eventEmitter.removeAllListeners('PubkyEvent');
    return ok(undefined);
  } catch (e) {
    return err(JSON.stringify(e));
  }
}
export async function auth(url, secretKey) {
  const res = await Pubky.auth(url, secretKey);
  if (res[0] === 'error') {
    return err(res[1]);
  }
  return ok(res[1]);
}
export async function parseAuthUrl(url) {
  try {
    const res = await Pubky.parseAuthUrl(url);
    if (res[0] === 'error') {
      return err(res[1]);
    }
    const parsed = JSON.parse(res[1]);
    return ok(parsed);
  } catch (e) {
    return err(JSON.stringify(e));
  }
}
export async function publish(recordName, recordContent, secretKey) {
  try {
    const res = await Pubky.publish(recordName, recordContent, secretKey);
    if (res[0] === 'error') {
      return err(res[1]);
    }
    return ok(res[1]);
  } catch (e) {
    return err(JSON.stringify(e));
  }
}
export async function resolve(publicKey) {
  try {
    const res = await Pubky.resolve(publicKey);
    if (res[0] === 'error') {
      return err(res[1]);
    }
    return ok(JSON.parse(res[1]));
  } catch (e) {
    return err(JSON.stringify(e));
  }
}

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