import JsUri from "jsuri";
import { default as Tab } from "./Tab";
import Yasr from "@matdata/yasr";
import { getAsValue } from "@matdata/yasgui-utils";
var getUrlParams = function (_url) {
    var urlFromWindow = false;
    if (!_url) {
        _url = window.location.href;
        urlFromWindow = true;
    }
    var url = new JsUri(_url);
    if (url.anchor().length > 0) {
        url.query(url.anchor());
        if (urlFromWindow)
            window.location.hash = "";
    }
    return url;
};
export function appendArgsToUrl(_url, args) {
    var url = new JsUri(_url);
    for (const arg in args) {
        const val = args[arg];
        if (Array.isArray(val)) {
            for (const subVal of val) {
                url.addQueryParam(arg, subVal);
            }
        }
        else {
            url.addQueryParam(arg, val);
        }
    }
    return url.toString();
}
export function createShareLink(forUrl, tab) {
    const currentUrl = new JsUri(forUrl);
    const tmpUrl = new JsUri();
    const configObject = createShareConfig(tab);
    let key;
    for (key in configObject) {
        if (!configObject.hasOwnProperty(key))
            continue;
        if (key === "namedGraphs") {
            configObject.namedGraphs.forEach((ng) => tmpUrl.addQueryParam("namedGraph", ng));
        }
        else if (key === "defaultGraphs") {
            configObject.defaultGraphs.forEach((dg) => tmpUrl.addQueryParam("defaultGraph", dg));
        }
        else if (key === "args") {
            const args = getAsValue(configObject.args, tab.yasgui);
            args.forEach((arg) => tmpUrl.addQueryParam(arg.name, arg.value));
        }
        else if (typeof configObject[key] === "object") {
            if (configObject[key])
                tmpUrl.addQueryParam(key, JSON.stringify(configObject[key]));
        }
        else {
            if (configObject[key])
                tmpUrl.addQueryParam(key, configObject[key]);
        }
    }
    const currentUrlParams = getUrlParams(forUrl);
    const currentParams = currentUrlParams.queryPairs;
    currentParams.forEach(function (paramPair) {
        if (!tmpUrl.hasQueryParam(paramPair[0])) {
            tmpUrl.addQueryParam(paramPair[0], paramPair[1]);
        }
    });
    currentUrl.anchor(tmpUrl.query().substr(1));
    return currentUrl.toString();
}
export function createShareConfig(tab) {
    const yasgui = tab.yasgui;
    const requestConfig = tab.getRequestConfig();
    const yasrPersistentSetting = tab.getPersistedJson().yasr.settings;
    return {
        query: tab.getQuery(),
        endpoint: tab.getEndpoint(),
        requestMethod: getAsValue(requestConfig.method, yasgui),
        tabTitle: tab.getName(),
        headers: getAsValue(requestConfig.headers, yasgui),
        contentTypeConstruct: getAsValue(requestConfig.acceptHeaderGraph, yasgui),
        contentTypeSelect: getAsValue(requestConfig.acceptHeaderSelect, yasgui),
        args: getAsValue(requestConfig.args, yasgui),
        namedGraphs: getAsValue(requestConfig.namedGraphs, yasgui),
        defaultGraphs: getAsValue(requestConfig.defaultGraphs, yasgui),
        outputFormat: yasrPersistentSetting.selectedPlugin,
        outputSettings: yasrPersistentSetting.pluginsConfig && yasrPersistentSetting.selectedPlugin
            ? yasrPersistentSetting.pluginsConfig[yasrPersistentSetting.selectedPlugin]
            : undefined,
    };
}
export function getConfigFromUrl(defaults, _url) {
    const options = defaults;
    var url = getUrlParams(_url);
    var hasQuery = false;
    const currentParams = url.queryPairs;
    var pluginsConfig;
    currentParams.forEach(function ([key, value]) {
        if (key === "query") {
            hasQuery = true;
            options.yasqe.value = value;
        }
        else if (key === "outputFormat" && value.length) {
            if (Yasr.plugins[value]) {
                options.yasr.settings.selectedPlugin = value;
            }
            else {
                console.warn(`Output format plugin "${value}" not found`);
            }
        }
        else if (key == "outputSettings") {
            pluginsConfig = JSON.parse(value);
        }
        else if (key == "contentTypeConstruct") {
            options.requestConfig.acceptHeaderGraph = value;
        }
        else if (key == "contentTypeSelect") {
            options.requestConfig.acceptHeaderSelect = value;
        }
        else if (key == "endpoint") {
            options.requestConfig.endpoint = value;
        }
        else if (key == "requestMethod") {
            options.requestConfig.method = value;
        }
        else if (key == "tabTitle") {
            options.name = value;
        }
        else if (key == "namedGraph") {
            if (!Array.isArray(options.requestConfig.namedGraphs))
                options.requestConfig.namedGraphs = [];
            options.requestConfig.namedGraphs.push(value);
        }
        else if (key == "defaultGraph") {
            if (!Array.isArray(options.requestConfig.defaultGraphs))
                options.requestConfig.defaultGraphs = [];
            options.requestConfig.defaultGraphs.push(value);
        }
        else if (key == "headers") {
            if (!options.requestConfig.headers)
                options.requestConfig.headers = {};
            options.requestConfig.headers = JSON.parse(value);
        }
        else {
            if (!options.requestConfig.args)
                options.requestConfig.args = [];
            options.requestConfig.args.push({ name: key, value: value });
        }
    });
    if (pluginsConfig && options.yasr.settings.selectedPlugin && options.yasr.settings.pluginsConfig) {
        options.yasr.settings.pluginsConfig[options.yasr.settings.selectedPlugin] = pluginsConfig;
    }
    if (hasQuery) {
        return options;
    }
}
export function queryCatalogConfigToTabConfig(catalogConfig, defaults) {
    const options = defaults || Tab.getDefaults();
    if (catalogConfig.service) {
        options.requestConfig.endpoint = catalogConfig.service;
    }
    if (catalogConfig.requestConfig) {
        if (catalogConfig.requestConfig.payload) {
            if (catalogConfig.requestConfig.payload.query) {
                options.yasqe.value = catalogConfig.requestConfig.payload.query;
            }
            if (catalogConfig.requestConfig.payload["default-graph-uri"]) {
                options.requestConfig.defaultGraphs = Array.isArray(catalogConfig.requestConfig.payload["default-graph-uri"])
                    ? catalogConfig.requestConfig.payload["default-graph-uri"]
                    : [catalogConfig.requestConfig.payload["default-graph-uri"]];
            }
            if (catalogConfig.requestConfig.payload["named-graph-uri"]) {
                options.requestConfig.namedGraphs = Array.isArray(catalogConfig.requestConfig.payload["named-graph-uri"])
                    ? catalogConfig.requestConfig.payload["named-graph-uri"]
                    : [catalogConfig.requestConfig.payload["named-graph-uri"]];
            }
        }
        if (catalogConfig.requestConfig.headers) {
            options.requestConfig.headers = catalogConfig.requestConfig.headers;
        }
    }
    if (catalogConfig.renderConfig) {
        if (catalogConfig.renderConfig.output) {
            if (Yasr.plugins[catalogConfig.renderConfig.output]) {
                options.yasr.settings.selectedPlugin = catalogConfig.renderConfig.output;
            }
            else {
                console.warn(`Output format plugin "${catalogConfig.renderConfig.output}" not found`);
            }
        }
        if (catalogConfig.renderConfig.settings) {
            if (Yasr.plugins[catalogConfig.renderConfig.output] && options.yasr.settings.pluginsConfig) {
                options.yasr.settings.pluginsConfig[catalogConfig.renderConfig.output] = catalogConfig.renderConfig.settings;
            }
            else {
                console.warn(`Output format plugin "${catalogConfig.renderConfig.output}" not found, cannot apply settings`);
            }
        }
    }
    if (catalogConfig.name) {
        options.name = catalogConfig.name;
    }
    return options;
}
//# sourceMappingURL=linkUtils.js.map