import { LazyResult } from 'postcss';
import { ParserPlugin } from '@babel/parser';

declare interface AssetURLOptions {
    [name: string]: string | string[];
}

declare type BindingMetadata = {
    [key: string]: BindingTypes | undefined;
} & {
    __isScriptSetup?: boolean;
};

declare const enum BindingTypes {
    /**
     * returned from data()
     */
    DATA = "data",
    /**
     * declared as a prop
     */
    PROPS = "props",
    /**
     * a local alias of a `<script setup>` destructured prop.
     * the original is stored in __propsAliases of the bindingMetadata object.
     */
    PROPS_ALIASED = "props-aliased",
    /**
     * a let binding (may or may not be a ref)
     */
    SETUP_LET = "setup-let",
    /**
     * a const binding that can never be a ref.
     * these bindings don't need `unref()` calls when processed in inlined
     * template expressions.
     */
    SETUP_CONST = "setup-const",
    /**
     * a const binding that does not need `unref()`, but may be mutated.
     */
    SETUP_REACTIVE_CONST = "setup-reactive-const",
    /**
     * a const binding that may be a ref.
     */
    SETUP_MAYBE_REF = "setup-maybe-ref",
    /**
     * bindings that are guaranteed to be refs
     */
    SETUP_REF = "setup-ref",
    /**
     * declared by other options, e.g. computed, inject
     */
    OPTIONS = "options"
}

/**
 * Compile `<script setup>`
 * It requires the whole SFC descriptor because we need to handle and merge
 * normal `<script>` + `<script setup>` if both are present.
 */
export declare function compileScript(sfc: SFCDescriptor, options?: ScriptCompileOptions): SFCScriptBlock;

export declare function compileStyle(options: StyleCompileOptions): StyleCompileResults;

export declare function compileStyleAsync(options: StyleCompileOptions): Promise<StyleCompileResults>;

export declare function compileTemplate(options: TemplateCompileOptions): TemplateCompileResult;

export declare function generateCodeFrame(source: string, start?: number, end?: number): string;

declare interface ImportBinding {
    isType: boolean;
    imported: string;
    source: string;
    isFromSetup: boolean;
    isUsedInTemplate: boolean;
}

export declare function parse(options: ParseOptions): SFCDescriptor;

declare interface ParseOptions {
    source: string;
    filename?: string;
    compiler?: VueTemplateCompiler;
    compilerParseOptions?: VueTemplateCompilerParseOptions;
    sourceRoot?: string;
    needMap?: boolean;
}

declare interface RawSourceMap extends StartOfSourceMap {
    version: string;
    sources: string[];
    names: string[];
    sourcesContent?: string[];
    mappings: string;
}

export declare interface ScriptCompileOptions {
    /**
     * Production mode. Used to determine whether to generate hashed CSS variables
     */
    isProd?: boolean;
    /**
     * Enable/disable source map. Defaults to true.
     */
    sourceMap?: boolean;
    /**
     * https://babeljs.io/docs/en/babel-parser#plugins
     */
    babelParserPlugins?: ParserPlugin[];
}

export declare interface SFCBlock extends SFCCustomBlock {
    lang?: string;
    src?: string;
    scoped?: boolean;
    module?: string | boolean;
}

export declare interface SFCCustomBlock {
    type: string;
    content: string;
    attrs: {
        [key: string]: string | true;
    };
    start: number;
    end: number;
    map?: RawSourceMap;
}

export declare interface SFCDescriptor {
    source: string;
    filename: string;
    template: SFCBlock | null;
    script: SFCScriptBlock | null;
    scriptSetup: SFCScriptBlock | null;
    styles: SFCBlock[];
    customBlocks: SFCCustomBlock[];
    errors: WarningMessage[];
    /**
     * compare with an existing descriptor to determine whether HMR should perform
     * a reload vs. re-render.
     *
     * Note: this comparison assumes the prev/next script are already identical,
     * and only checks the special case where `<script setup lang="ts">` unused
     * import pruning result changes due to template changes.
     */
    shouldForceReload: (prevImports: Record<string, ImportBinding>) => boolean;
}

declare interface SFCScriptBlock extends SFCBlock {
    type: 'script';
    setup?: string | boolean;
    bindings?: BindingMetadata;
    imports?: Record<string, ImportBinding>;
    /**
     * import('\@babel/types').Statement
     */
    scriptAst?: any[];
    /**
     * import('\@babel/types').Statement
     */
    scriptSetupAst?: any[];
}

declare interface StartOfSourceMap {
    file?: string;
    sourceRoot?: string;
}

export declare interface StyleCompileOptions {
    source: string;
    filename: string;
    id: string;
    map?: any;
    scoped?: boolean;
    trim?: boolean;
    preprocessLang?: string;
    preprocessOptions?: any;
    postcssOptions?: any;
    postcssPlugins?: any[];
}

export declare interface StyleCompileResults {
    code: string;
    map: any | void;
    rawResult: LazyResult | void;
    errors: string[];
}

export declare interface TemplateCompileOptions {
    source: string;
    filename: string;
    compiler?: VueTemplateCompiler;
    compilerOptions?: VueTemplateCompilerOptions;
    transformAssetUrls?: AssetURLOptions | boolean;
    transformAssetUrlsOptions?: TransformAssetUrlsOptions;
    preprocessLang?: string;
    preprocessOptions?: any;
    transpileOptions?: any;
    isProduction?: boolean;
    isFunctional?: boolean;
    optimizeSSR?: boolean;
    prettify?: boolean;
    isTS?: boolean;
}

export declare interface TemplateCompileResult {
    ast: Object | undefined;
    code: string;
    source: string;
    tips: (string | WarningMessage)[];
    errors: (string | WarningMessage)[];
}

declare interface TransformAssetUrlsOptions {
    /**
     * If base is provided, instead of transforming relative asset urls into
     * imports, they will be directly rewritten to absolute urls.
     */
    base?: string;
}

declare interface VueTemplateCompiler {
    parseComponent(source: string, options?: any): SFCDescriptor;
    compile(template: string, options: VueTemplateCompilerOptions): VueTemplateCompilerResults;
    ssrCompile(template: string, options: VueTemplateCompilerOptions): VueTemplateCompilerResults;
}

declare interface VueTemplateCompilerOptions {
    modules?: Object[];
    outputSourceRange?: boolean;
    whitespace?: 'preserve' | 'condense';
    directives?: {
        [key: string]: Function;
    };
}

declare interface VueTemplateCompilerParseOptions {
    pad?: 'line' | 'space' | boolean;
    deindent?: boolean;
    outputSourceRange?: boolean;
}

declare interface VueTemplateCompilerResults {
    ast: Object | undefined;
    render: string;
    staticRenderFns: string[];
    errors: (string | WarningMessage)[];
    tips: (string | WarningMessage)[];
}

declare type WarningMessage = {
    msg: string;
    start?: number;
    end?: number;
};

export { }
