import { readdir } from "fs/promises";
import { join } from "path";
import { createHash } from "crypto";

const logosDir = import.meta.dev
  ? join(process.cwd(), "public/logos")
  : join(process.cwd(), ".output/public/logos");

export const getLogoNames = cachedFunction(
  async () => {
    const files = await readdir(logosDir);
    return files
      .filter((f) => f.endsWith(".png"))
      .map((f) => f.replace(/\.png$/, ""))
      .sort();
  },
  {
    maxAge: 60 * 60 * 24,
    name: "logoNames",
    getKey: async () => {
      const files = await readdir(logosDir);
      const key = files
        .filter((f) => f.endsWith(".png"))
        .sort()
        .join(",");
      return createHash("sha256").update(key).digest("hex");
    },
  },
);
