class JavaScriptTasksAssistant {
    resolveTaskAction(context) {
        if (context.action != Task.Run) {
            return null;
        }
        
        let data = context.data;
        let config = context.config;
        let root = nova.workspace.path;
        let adapterPath = nova.path.normalize(nova.path.join(nova.extension.path, "../../NovaJSDebugAdapter"));
        let isLaunch = config.get("request", "string") == "launch";
        
        switch (data.type) {
        case "chromeDebug": {
            let action = new TaskDebugAdapterAction("chrome");
            action.command = adapterPath;
            action.debugRequest = isLaunch ? "launch" : "attach";
            if (isLaunch) {
                let userDataDir = nova.path.join(nova.extension.globalStoragePath, "chrome-debug-profile");
                action.debugArgs = {
                    type: "browser",
                    program: resolveRelative(config.get("launchPath", "string"), root),
                    args: config.get("launchArgs", "array"),
                    port: config.get("port", "number"),
                    stopOnEntry: config.get("stopOnEntry", "boolean"),
                    sourceRoot: resolveRelative(config.get("sourceRoot", "string"), root) || nova.workspace.previewRootPath,
                    sourceURL: config.get("sourceURL", "string") || nova.workspace.previewURL,
                    userDataDir: userDataDir
                };
            }
            else {
                action.debugArgs = {
                    type: "browser",
                    port: config.get("port", "number"),
                    stopOnEntry: config.get("stopOnEntry", "boolean"),
                    sourceURL: config.get("sourceURL", "string") || nova.workspace.previewURL,
                    sourceRoot: resolveRelative(config.get("sourceRoot", "string"), root) || nova.workspace.previewRootPath
                };
            }
            return action;
        }
        case "denoDebug": {
            let action = new TaskDebugAdapterAction("deno");
            action.command = adapterPath;
            action.debugRequest = isLaunch ? "launch" : "attach";
            if (isLaunch) {
                action.debugArgs = {
                    type: "deno",
                    program: config.get("scriptPath", "string"),
                    args: config.get("launchArgs", "array"),
                    cwd: root,
                    runtime: config.get("runtimeExecutable", "string"),
                    runtimeArgs: config.get("runtimeArgs", "array"),
                    host: config.get("host", "string"),
                    port: config.get("port", "number"),
                    stopOnEntry: config.get("stopOnEntry", "boolean"),
                    sourceRoot: root,
                    pathMappings: (config.get("pathMappings") || []).map(o => {
                        return {
                            local: resolveRelative(o.localRoot, root) || root,
                            remote: o.remoteRoot
                        };
                    })
                };
            }
            else {
                action.debugArgs = {
                    type: "deno",
                    host: config.get("host", "string"),
                    port: config.get("port", "number"),
                    stopOnEntry: config.get("stopOnEntry", "boolean"),
                    sourceRoot: root,
                    pathMappings: (config.get("pathMappings") || []).map(o => {
                        return {
                            local: resolveRelative(o.localRoot, root) || root,
                            remote: o.remoteRoot
                        };
                    })
                };
            }
            return action;
        }
        case "nodeDebug": {
            let action = new TaskDebugAdapterAction("node");
            action.command = adapterPath;
            action.debugRequest = isLaunch ? "launch" : "attach";
            if (isLaunch) {
                action.debugArgs = {
                    type: "node",
                    program: config.get("scriptPath", "string"),
                    args: config.get("launchArgs", "array"),
                    cwd: root,
                    runtime: config.get("runtimeExecutable", "string"),
                    runtimeArgs: config.get("runtimeArgs", "array"),
                    host: config.get("host", "string"),
                    port: config.get("port", "number"),
                    stopOnEntry: config.get("stopOnEntry", "boolean"),
                    sourceRoot: root,
                    pathMappings: (config.get("pathMappings") || []).map(o => {
                        return {
                            local: resolveRelative(o.localRoot, root) || root,
                            remote: o.remoteRoot
                        };
                    })
                };
            }
            else {
                action.debugArgs = {
                    type: "node",
                    host: config.get("host", "string"),
                    port: config.get("port", "number"),
                    stopOnEntry: config.get("stopOnEntry", "boolean"),
                    sourceRoot: root,
                    pathMappings: (config.get("pathMappings") || []).map(o => {
                        return {
                            local: resolveRelative(o.localRoot, root) || root,
                            remote: o.remoteRoot
                        };
                    })
                };
            }
            return action;
        }
        default:
            return null;
        }
    }
}

function resolveRelative(path, to) {
    if (!path || nova.path.isAbsolute(path)) {
        return path;
    }
    else {
        return nova.path.join(to, path);
    }
}

module.exports = new JavaScriptTasksAssistant();
