import { needleAsap } from "./asap.js";
export { needleAsap } from "./asap.js";

import { needleDefines } from "./defines.js";
export { needleDefines } from "./defines.js";

import { needleBuildPipeline } from "./build-pipeline.js";
export { needleBuildPipeline } from "./build-pipeline.js";

import { needleBuild } from "./build.js";
export { needleBuild } from "./build.js";

import { needleMeta } from "./meta.js"
export { needleMeta } from "./meta.js"

import { needlePoster } from "./poster.js"
export { needlePoster } from "./poster.js"

import { needlePWA } from "./pwa.js";
export { needlePWA } from "./pwa.js";

import { needleReload } from "./reload.js"
export { needleReload } from "./reload.js"

import { needleDrop } from "./drop.js";
export { needleDrop } from "./drop.js";

import { editorConnection } from "./editor-connection.js";
export { editorConnection } from "./editor-connection.js";

import { needleCopyFiles } from "./copyfiles.js";
export { needleCopyFiles } from "./copyfiles.js";

import { needleViteAlias } from "./alias.js";
export { needleViteAlias } from "./alias.js";

import { needleTransformCodegen } from "./transform-codegen.js";
export { needleTransformCodegen } from "./transform-codegen.js";

import { needleLicense } from "./license.js";
export { needleLicense } from "./license.js";

import { needleMakeFilesLocal } from "./local-files.js";
export { needleMakeFilesLocal } from "./local-files.js";

import { needlePeerjs } from "./peer.js";
export { needlePeerjs } from "./peer.js";

import { needleDependencyWatcher } from "./dependency-watcher.js";
export { needleDependencyWatcher } from "./dependency-watcher.js";

import { needleDependencies } from "./dependencies.js";
export { needleDependencies } from "./dependencies.js";

import { needleFacebookInstantGames } from "./facebook-instant-games.js";
export { needleFacebookInstantGames } from "./facebook-instant-games.js";

import { vite_4_4_hack } from "./vite-4.4-hack.js";

import { needleImportsLogger } from "./imports-logger.js";
export { needleImportsLogger } from "./imports-logger.js";

import { needleBuildInfo } from "./buildinfo.js";
export { needleBuildInfo } from "./buildinfo.js";

import { needleServer } from "./server.js";
import { needleNPM } from "./npm.js";
import { needleTransformCode } from "./transform.js";
import { needleMaterialXLoader } from "./materialx.js";
import { needleLogger } from "./logger.js";
import { needleApp } from "./needle-app.js";
import { viteFixWorkerImport } from "../common/worker.js";
export { needleServer } from "./server.js";


export * from "./gzip.js";
export * from "./config.js";

const defaultUserSettings = {
    allowRemoveMetaTags: true,
    allowHotReload: true,
}

/** # Needle Engine plugins for Vite
 * Plugins include hot reload support, meta tags, defines, build pipeline, PWA, and more.
 * ## Using PWA
 * How to add PWA support to your vite project:
 * 1) Install the [vite pwa plugin](https://vite-pwa-org.netlify.app/): `npm install vite-plugin-pwa --save-dev`
 * 2) Add the following to your vite.config.js:  
 *   You first pass the PWAOptions to the needlePlugins function, then you pass the same PWAOptions to the VitePWA plugin.
 * You *can* use also add a `.webmanifest` file to your web project and edit the [PWA manifest](https://developer.mozilla.org/en-US/docs/Web/Manifest) settings there.
* ```js
 * export default defineConfig(async ({ command }) => {
    // \@type(import("vite-plugin-pwa").VitePWAOptions) (remove the backslash)
    const PWAOptions = {};
    ...
    return {
        plugins: [
            ...
            needlePlugins(command, needleConfig, { pwaOptions: PWAOptions }),
            VitePWA(PWAOptions),
        ],
    }
 * ```
 * @param {"build" | "serve"} command
 * @param {{} | undefined | null} config
 * @param {import('../types/index.js').userSettings} userSettings
 */
export const needlePlugins = async (command, config = undefined, userSettings = {}) => {

    if (!config) config = {}

    // ensure we have user settings initialized with defaults
    userSettings = { ...defaultUserSettings, ...userSettings };

    const array = [
        needleLogger(command, config, userSettings),
        needleDefines(command, config, userSettings),
        needleLicense(command, config, userSettings),
        needleViteAlias(command, config, userSettings),
        needleMeta(command, config, userSettings),
        needlePoster(command, config, userSettings),
        needleReload(command, config, userSettings),
        needleMakeFilesLocal(command, config, userSettings),
        needleBuild(command, config, userSettings),
        needleBuildInfo(command, config, userSettings),
        needleCopyFiles(command, config, userSettings),
        needleTransformCodegen(command, config, userSettings),
        needleTransformCode(command, config, userSettings),
        needleDrop(command, config, userSettings),
        needlePeerjs(command, config, userSettings),
        needleDependencyWatcher(command, config, userSettings),
        needleDependencies(command, config, userSettings),
        vite_4_4_hack(command, config, userSettings),
        needleFacebookInstantGames(command, config, userSettings),
        needleImportsLogger(command, config, userSettings),
        needleBuildPipeline(command, config, userSettings),
        needlePWA(command, config, userSettings),
        needleServer(command, config, userSettings),
        needleNPM(command, config, userSettings),
        needleMaterialXLoader(command, config, userSettings),
        needleApp(command, config, userSettings),
        viteFixWorkerImport()
    ];

    const asap = await needleAsap(command, config, userSettings);
    if(asap) array.push(asap);

    array.push(await editorConnection(command, config, userSettings, array));
    return array;
}
