import * as IDB from "idb-keyval";

import type { Server } from "./types";
import { IDB_SERVERS } from "./constants";

export async function loadServers(): Promise<Record<string, Server>> {
  const i = await IDB.get(IDB_SERVERS);
  return i ? i : {};
}

export async function saveServers(items: Record<string, Server>) {
  await IDB.set(IDB_SERVERS, items);
}

export function serverId(server: Server) {
  if (server.apiKey) return `${server.apiKey}@${server.host}`;
  return `${server.username}:${server.password}@${server.host}`;
}
