import * as url from 'url'
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import * as prettier from 'prettier'

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

const labelsDef = JSON.parse(
  readFileSync(
    join(__dirname, '..', '..', 'definitions', 'labels.json'),
    'utf8',
  ),
)

writeFileSync(
  join(__dirname, '..', '..', 'src', 'moderation', 'const', 'labels.ts'),
  await gen(),
  'utf8',
)

async function gen() {
  return prettier.format(
    `/** this doc is generated by ./scripts/code/labels.mjs **/
  import {InterpretedLabelValueDefinition, LabelPreference} from '../types'

  export type KnownLabelValue = ${labelsDef
    .map((label) => `"${label.identifier}"`)
    .join(' | ')}

  export const DEFAULT_LABEL_SETTINGS: Record<string, LabelPreference> = ${JSON.stringify(
    Object.fromEntries(
      labelsDef
        .filter((label) => label.configurable)
        .map((label) => [label.identifier, label.defaultSetting]),
    ),
  )}

  export const LABELS: Record<KnownLabelValue, InterpretedLabelValueDefinition> = ${JSON.stringify(
    Object.fromEntries(
      labelsDef.map((label) => [label.identifier, { ...label, locales: [] }]),
    ),
    null,
    2,
  )}
  `,
    { semi: false, parser: 'typescript', singleQuote: true },
  )
}

export {}
