import type { DB } from "../generated/kysely-tailordb";
import { ok, type ReadonlyDB } from "@tailor-platform/erp-kit/module";

export type GetCurrencyInput = { id: string } | { code: string };

export async function run(db: ReadonlyDB<DB>, input: GetCurrencyInput) {
  let query = db.selectFrom("Currency").selectAll();

  if ("id" in input) {
    query = query.where("id", "=", input.id);
  } else {
    query = query.where("code", "=", input.code);
  }

  const currency = await query.executeTakeFirst();

  return ok({ currency: currency ?? null });
}
