import { callAuthEndpoint } from "./http";
import {
  createPasskeyCredential,
  getPasskeyCredential,
  type PasskeyAuthenticatorAttachment,
} from "./passkey";
import {
  PasskeyModule,
  type PasskeyModuleDependencies,
  type PasskeyRegisterInput,
} from "./passkey-module";

export * from "./passkey-types";
export { PasskeyModule };
export type { PasskeyAuthenticatorAttachment, PasskeyModuleDependencies, PasskeyRegisterInput };

const defaultPasskeyModule = new PasskeyModule({
  callEndpoint: callAuthEndpoint,
  credentialAdapter: {
    createCredential: createPasskeyCredential,
    getCredential: getPasskeyCredential,
  },
});

export function createPasskeyModule(
  deps: PasskeyModuleDependencies = {
    callEndpoint: callAuthEndpoint,
    credentialAdapter: {
      createCredential: createPasskeyCredential,
      getCredential: getPasskeyCredential,
    },
  },
): PasskeyModule {
  return new PasskeyModule(deps);
}

export function getPasskeyModule(): PasskeyModule {
  return defaultPasskeyModule;
}

export async function listUserPasskeys() {
  return defaultPasskeyModule.listUserPasskeys();
}

export async function registerPasskeyViaApi(args: PasskeyRegisterInput) {
  return defaultPasskeyModule.registerPasskey(args);
}

export async function authenticateWithPasskeyViaApi() {
  return defaultPasskeyModule.authenticateWithPasskey();
}

export async function renamePasskeyViaApi(id: string, name: string) {
  return defaultPasskeyModule.renamePasskey(id, name);
}

export async function deletePasskeyViaApi(id: string) {
  return defaultPasskeyModule.deletePasskey(id);
}

export function buildPasskeyRenameDrafts(
  passkeys: Parameters<PasskeyModule["buildRenameDrafts"]>[0],
  currentDrafts: Record<string, string>,
): Record<string, string> {
  return defaultPasskeyModule.buildRenameDrafts(passkeys, currentDrafts);
}
