import type {Plugin, OutputChunk, OutputAsset, OutputBundle, TransformModuleJSON, } from 'rollup';
import {FilterPattern} from "@rollup/pluginutils";
import type {DefaultTreeAdapterMap} from "parse5";
import {PreRenderedChunk} from "rollup";

import type {LoadNodeCallback} from "./load.d.ts";
export type * from "./load.d.ts"

import type {ResolveCallback} from "./resolve.d.ts";
export type * from "./resolve.d.ts"

export interface RollupHtmlTransformContext {
  id?: string;
  // bundle: OutputBundle;
  // files: Record<string, (OutputChunk | OutputAsset)[]>;
}

export interface RewriteUrlCallbackContext {
  from: string;
  rootPath: string;
}
export type RewriteUrlCallback = (relative: string, context: RewriteUrlCallbackContext) => string|Promise<string>;
export type TransformCallback = (source: string, transformContext: RollupHtmlTransformContext) => string|Promise<string>;

export interface RollupHtmlOptions {
  publicPath?: string;
  /**
   * Follows the same logic as rollup's [entryFileNames](https://rollupjs.org/configuration-options/#output-entryfilenames).
   */
  htmlFileNames?: string|((chunkInfo: PreRenderedChunk) => string);

  /**
   * Transform a source file passed into this plugin to HTML. For example: a handlebars transform
   * ```
   *    transform(source){
   *      return handlebars.compile(source)({myVar:'example'})
   *    }
   * ```
   */
  transform?: TransformCallback;

  /**
   * Optional callback to rewrite how resources are referenced in the output HTML.
   * For example to rewrite urls to as paths from the root of your website:
   * ```
   *    rewriteUrl(relative, {rootPath, from}){
   *      return `/${rootPath}`;
   *    }
   * ```
   */
  rewriteUrl?: RewriteUrlCallback;

  /**
   * Detect which references (<a href="...">, <img src="...">) to resolve from a HTML node.
   * This rarely needs to be overloaded, but can be used to support non-native attributes used by custom-elements.
   *
   * Return false to skip any further processing on this node. Use the load function to add any resources from this node, and replace the import with a placeholder so the plugin knows where to inject the end result
   */
  load?: LoadNodeCallback;
  /**
   * Callback to filter which references actually need to be resolved. Here you can filter out:
   *   - Links to extensions that don't need to be handled through rollup
   *   - Resources that are external to the app (for example non-relative paths)
   *   - Page navigation within the app
   *
   * Return a falsey value to skip this reference. Return true to resolve as is. (or string to transform the id)
   */
  resolve?: ResolveCallback;

  /**
   * [Pattern](https://github.com/micromatch/picomatch#globbing-features) to include
   */
  include?: FilterPattern;
  /**
   * [Pattern](https://github.com/micromatch/picomatch#globbing-features) to exclude
   */
  exclude?: FilterPattern
}


/**
 * A Rollup plugin which creates HTML files to serve Rollup bundles.
 * @param options - Plugin options.
 * @returns Plugin instance.
 */
export default function html(options?: RollupHtmlOptions): Plugin;
