// @ts-check

import { generate } from '@graphql-codegen/cli'
import { readdir } from 'fs/promises'

const listedGql = (await readdir('schemas/')).filter(item => /.*\.graphql/.exec(item)).filter(item => !item.startsWith('_'))
const schemas = Object.fromEntries(listedGql.map(schema => [`./generated/urql/subschemas/${schema.split('.')[0]}Type.ts`, {
  schema: `schemas/${schema}`,
  plugins: ["typescript", "typescript-resolvers", {
    add: {
      content: '/* eslint-disable */',
    },
  },
  {
    add: {
      content: '//@ts-nocheck',
    },
  },],
}]))

await generate(
    {
        cwd: process.cwd(),
        verbose: true,
        noSilentErrors: true,
        ignoreNoDocuments: true, // for better experience with the watcher
        config: {
          scalars: {
            DateTime: 'string',
          },
          skipTypename: true,
          avoidOptionals: true,
          enumsAsTypes: true,
          futureProofEnums: true,
          dedupeOperationSuffix: true,
          useTypeImports: true,
        },
        generates: {
          ...schemas,
          './generated/schema.json': {
            schema: ['schemas/schema.json', 'schemas/*.graphql'],
            plugins: ['introspection'],
            config: {
              minify: false,
            },
          },
          './generated/schema.remote.json': {
            schema: ['schemas/schema.json'],
            plugins: ['introspection'],
          },
          './generated/schema.graphql': {
            schema: ['schemas/schema.json', 'schemas/*.graphql'],
            plugins: ['schema-ast'],
          },
          './generated/urql/cache-typing.ts': {
            schema: ['schemas/schema.json', 'schemas/*.graphql'],
            plugins: [
              'typescript',
              'typescript-urql-graphcache',
              {
                add: {
                  content: '/* eslint-disable */',
                },
              },
            ],
            config: {
              offlineExchange: true,
              nonOptionalTypename: true,
              skipTypename: false,
              useTypeImports: true,
            }
          },
        },
      },
    true
  )