import * as _nuxt_schema from '@nuxt/schema';
import { HookResult, Nuxt } from '@nuxt/schema';
import { createAdminApiClient } from '@shopify/admin-api-client';
import { createStorefrontApiClient } from '@shopify/storefront-api-client';
import { ConsolaOptions } from 'consola';

type StorefrontOptions = Parameters<typeof createStorefrontApiClient>[0]
type AdminOptions = Parameters<typeof createAdminApiClient>[0]

// The supported client types
declare enum ShopifyClientType {
    Storefront = 'storefront',
    Admin = 'admin',
}

// custom options for each client
// exposed via module options
type ShopifyClientCustomConfig = {
    skipCodegen?: boolean
    sandbox?: boolean // defaults to true
    documents?: string[]
}

type ShopifyStorefrontConfig = StorefrontOptions & ShopifyClientCustomConfig
type ShopifyAdminConfig = AdminOptions & ShopifyClientCustomConfig

// Fully resolved shopify config
type ShopifyConfig<S = ShopifyStorefrontConfig, A = ShopifyAdminConfig> = {
    name: string
    logger?: Partial<ConsolaOptions>
    clients: {
        [ShopifyClientType.Storefront]?: S
        [ShopifyClientType.Admin]?: A
    }
}

type ModuleOptions = ShopifyConfig<
    Omit<ShopifyStorefrontConfig, 'storeDomain'>,
    Omit<ShopifyAdminConfig, 'storeDomain'>
>

type ShopifyConfigHookParams = {
    nuxt: Nuxt
    config: ShopifyConfig
}

type ShopifyTemplateHookParams = {
    nuxt: Nuxt
    config: Record<string, unknown>
}

type SandboxConfig = {
    url: string
    headers: Record<string, string>
}

declare module '@nuxt/schema' {
    interface RuntimeConfig {
        _shopify?: ShopifyConfig
        _sandbox?: Record<string, SandboxConfig>
    }

    interface NuxtHooks {
        /**
         * Called before the config is persisted into the runtime config
         */
        'shopify:config': ({ nuxt, config }: ShopifyConfigHookParams) => HookResult

        /**
         * Called before the storefront introspection schema is generated
         */
        'storefront:generate:introspection': ({ nuxt, config }: ShopifyTemplateHookParams) => HookResult

        /**
         * Called before the admin introspection schema is generated
         */
        'admin:generate:introspection': ({ nuxt, config }: ShopifyTemplateHookParams) => HookResult

        /**
         * Called before the storefront types are generated
         */
        'storefront:generate:types': ({ nuxt, config }: ShopifyTemplateHookParams) => HookResult

        /**
         * Called before the admin types are generated
         */
        'admin:generate:types': ({ nuxt, config }: ShopifyTemplateHookParams) => HookResult

        /**
         * Called before the storefront operations are generated
         */
        'storefront:generate:operations': ({ nuxt, config }: ShopifyTemplateHookParams) => HookResult

        /**
         * Called before the admin operations are generated
         */
        'admin:generate:operations': ({ nuxt, config }: ShopifyTemplateHookParams) => HookResult
    }
}

declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;

export { _default as default };
