import { LintModule } from '@microsoft/windows-admin-center-sdk/tools/code-formatter/lint';
import { parallel, series, watch } from 'gulp';
import { CleanModule } from './common/clean';
import { CompileModule } from './common/compile';
import { Config } from './common/config';
import { CopyModule } from './common/copy';
import { PackModule } from './common/pack';
import { PowerShellModule } from './common/powershell';
import { ResjsonModule } from './common/resjson';
import { TestModule } from './common/test';
import { UITestAutomationModule } from './common/ui-test-automation';
import { ValidateModule } from './common/validate';
import { gulpConfig } from './config-data';

module IndexModule {
    const config: Config = gulpConfig();

    // Export tasks
    export const clean = CleanModule.clean;
    export const lintApp = LintModule.lintApp;
    export const validateManifest = ValidateModule.validateManifest;
    export const validatePsCimConfig = ValidateModule.validatePsCimConfig;
    export const validate = config.powershell.skipCim ? validateManifest : parallel(validateManifest, validatePsCimConfig);
    export const resjson = ResjsonModule.resjson;
    export const powershell = config.powershell.skip ? function powershellSkip(cb) { cb(); } : PowerShellModule.powershell;
    export const copy = CopyModule.copy;
    export const compileLib = config.build?.library ? CompileModule.compileLib : function compileLibSkip(cb) { cb(); };
    export const bundleApp = CompileModule.bundleApp;
    export const serveApp = CompileModule.serveApp;
    export const ut = TestModule.unitTestApp;
    export const pester = TestModule.pester;
    export const test = TestModule.test;
    export const pack = PackModule.pack;

    // Build Tasks
    export const generate = parallel(resjson, powershell);
    export const app = series(lintApp, bundleApp);
    export const build = series(clean, generate, validate, LintModule.lintApp, compileLib, copy, test, bundleApp);
    export const quick = series(clean, generate, compileLib, copy, bundleApp);

    // Serve Tasks
    export function watchResource(cb) {
        watch(['src/resources/**/*.resjson', 'src/resources/**/*.ps1'], { ignoreInitial: false }, generate);
        cb();
    }

    export const serve = series(watchResource, serveApp);

    /* UI Test Automation tasks */
    // New UTA format
    export const watchUTA = series(
        LintModule.lintUiTestAutomation,
        UITestAutomationModule.buildLib,
        UITestAutomationModule.saveUiTestAutomation,
        UITestAutomationModule.saveUiTestAutomationToTarget
    );
    export function watchUiTestAutomation(cb) {
        watch(
            ['uta/**/*.*'],
            { ignoreInitial: false, delay: 1000 },
            watchUTA);
        cb();
    }

    // Legacy UTA format
    // TODO: cleanup _legacy functions when fully migrated to new format
    export const watchUTA_legacy = series(
        LintModule.lintUiTestAutomation_legacy,
        UITestAutomationModule.buildUiTestAutomation,
        UITestAutomationModule.saveUiTestAutomation_legacy
    );
    export function watchUiTestAutomation_legacy(cb) {
        watch(
            ['ui-test-automation/**/*.*'],
            { ignoreInitial: false, delay: 1000 },
            watchUTA_legacy);
        cb();
    }
}

// ui-test-automation aliases
// New UTA format
IndexModule['uta-build'] = series(
    LintModule.lintUiTestAutomation,
    UITestAutomationModule.buildLib,
    UITestAutomationModule.copyLibPackageJson
);
IndexModule['uta-save'] = series(UITestAutomationModule.saveUiTestAutomation, UITestAutomationModule.saveUiTestAutomationToTarget);
IndexModule['uta-watch'] = IndexModule.watchUiTestAutomation;

// Legacy UTA format
// TODO: cleanup all -legacy aliases when fully migrated to new format
IndexModule['uta-build-legacy'] = series(LintModule.lintUiTestAutomation_legacy, UITestAutomationModule.buildUiTestAutomation);
IndexModule['uta-save-legacy'] = UITestAutomationModule.saveUiTestAutomation_legacy;
IndexModule['uta-watch-legacy'] = IndexModule.watchUiTestAutomation_legacy;

export = IndexModule;
