#!/usr/bin/env -S node -r esbin
import fs from "fs"
import { uniq } from "lodash"
import { writeFormatted } from "./utils"

const modules = [
  "src/vendor/allotment/src/allotment.module.css",
  "src/vendor/allotment/src/sash.module.css",
]

for (const module of modules) {
  const source = fs.readFileSync(module, "utf8")
  const classNames = uniq(
    Array.from(source.matchAll(/\.([a-z][a-z0-9_-]+)/gi), m => m[1]),
  )

  writeFormatted(
    module + ".d.ts",
    [
      "// This file is generated by scripts/build-css-module-def.ts",
      "",
      "declare const classNames: {", //
      ...classNames.map(c => `  "${c}": string;`),
      "}",
      "",
      "export default classNames;",
    ].join("\n"),
  )
}
