import type { Context } from '@bamboocss/core'
import { outdent } from 'outdent'

export const generateTypesEntry = (ctx: Context) => {
  const indexExports = [
    // We need to export types used in the global.d.ts here to avoid TS errors such as `The inferred type of 'xxx' cannot be named without a reference to 'yyy'`
    `import '${ctx.file.extDts('./global')}'`,
    ctx.file.exportTypeStar('./conditions'),
    ctx.file.exportTypeStar('./pattern'),
    ctx.file.exportTypeStar('./recipe'),
    ctx.file.exportTypeStar('./system-types'),
    ctx.file.exportTypeStar('./style-props'),
  ].filter(Boolean)

  return {
    global: outdent`
    // @ts-nocheck
    import type * as Bamboo from '@bamboocss/dev'
    ${ctx.file.importType('RecipeVariantRecord, RecipeConfig, SlotRecipeVariantRecord, SlotRecipeConfig', './recipe')}
    ${ctx.file.importType('PatternConfig, PatternProperties', './pattern')}
    ${ctx.file.importType('GlobalStyleObject, SystemStyleObject', './system-types')}
    ${ctx.file.importType('CompositionStyles', './composition')}

    declare module '@bamboocss/dev' {
      export function defineRecipe<V extends RecipeVariantRecord>(config: RecipeConfig<V>): Bamboo.RecipeConfig
      export function defineSlotRecipe<S extends string, V extends SlotRecipeVariantRecord<S>>(config: SlotRecipeConfig<S, V>): Bamboo.SlotRecipeConfig
      export function defineStyles(definition: SystemStyleObject): SystemStyleObject
      export function defineGlobalStyles(definition: GlobalStyleObject): Bamboo.GlobalStyleObject
      export function defineMixins(definition: CompositionStyles['mixins']): Bamboo.Mixins
      export function definePattern<T extends PatternProperties>(config: PatternConfig<T>): Bamboo.PatternConfig
    }
    `,
    index: outdent`
    ${indexExports.join('\n')}
    `,
    helpers: outdent`
    export type Pretty<T> = T extends infer U ? { [K in keyof U]: U[K] } : never
    `,
  }
}
