import { removeProperty, setProperty } from '@sqs/jsonc-parser/lib/edit';
import { parseJSON } from '../settings/configuration';
const defaultFormattingOptions = {
    eol: '\n',
    insertSpaces: true,
    tabSize: 2,
};
const addGitHubDotCom = config => {
    const tokenPlaceholder = '<personal access token with repo scope (https://github.com/settings/tokens/new)>';
    const value = {
        token: tokenPlaceholder,
        url: 'https://github.com',
    };
    const edits = setProperty(config, ['github', -1], value, defaultFormattingOptions);
    return { edits, selectText: tokenPlaceholder };
};
const addGitHubEnterprise = config => {
    const tokenPlaceholder = '<personal access token with repo scope>';
    const value = {
        token: tokenPlaceholder,
        url: 'https://github-enterprise-hostname.example.com',
    };
    const edits = setProperty(config, ['github', -1], value, defaultFormattingOptions);
    return { edits, selectText: tokenPlaceholder };
};
const addGitLab = config => {
    const tokenPlaceholder = '<personal access token with api scope (https://[your-gitlab-hostname]/profile/personal_access_tokens)>';
    const value = {
        url: 'https://gitlab.example.com',
        token: tokenPlaceholder,
    };
    const edits = setProperty(config, ['gitlab', -1], value, defaultFormattingOptions);
    return { edits, selectText: tokenPlaceholder };
};
const addBitbucketServer = config => {
    const tokenPlaceholder = '<personal access token with read scope (https://[your-bitbucket-hostname]/plugins/servlet/access-tokens/add)>';
    const value = {
        url: 'https://bitbucket.example.com',
        token: tokenPlaceholder,
    };
    const edits = setProperty(config, ['bitbucketServer', -1], value, defaultFormattingOptions);
    return { edits, selectText: tokenPlaceholder };
};
const addAWSCodeCommit = config => {
    const value = {
        region: '',
        accessKeyID: '',
        secretAccessKey: '',
    };
    const edits = setProperty(config, ['awsCodeCommit', -1], value, defaultFormattingOptions);
    return { edits, selectText: '""', cursorOffset: 1 };
};
const addOtherRepository = config => {
    const urlPlaceholder = '<git clone URL>';
    const value = {
        url: urlPlaceholder,
        path: '<desired name of repository on Sourcegraph (example: my/repo)>',
    };
    const edits = setProperty(config, ['repos.list', -1], value, defaultFormattingOptions);
    return { edits, selectText: urlPlaceholder };
};
const addGSuiteOIDCAuthProvider = config => {
    const value = {
        type: 'openidconnect',
        issuer: 'https://accounts.google.com',
        clientID: '<see documentation: https://developers.google.com/identity/protocols/OpenIDConnect#getcredentials>',
        clientSecret: '<see same documentation as clientID>',
        requireEmailDomain: "<your company's email domain (example: mycompany.com)>",
    };
    return {
        edits: [
            ...removeProperty(config, ['auth.provider'], defaultFormattingOptions),
            ...setProperty(config, ['auth.providers'], [value], defaultFormattingOptions),
        ],
    };
};
const addSAMLAuthProvider = config => {
    const value = {
        type: 'saml',
        identityProviderMetadataURL: '<see https://about.sourcegraph.com/docs/server/config/authentication#saml>',
    };
    return {
        edits: [
            ...removeProperty(config, ['auth.provider'], defaultFormattingOptions),
            ...setProperty(config, ['auth.providers'], [value], defaultFormattingOptions),
        ],
    };
};
const addLicenseKey = config => {
    const value = '<input a license key generated from /site-admin/license. See https://about.sourcegraph.com/pricing for more details>';
    const edits = setProperty(config, ['licenseKey'], value, defaultFormattingOptions);
    return { edits, selectText: value };
};
const addSearchScopeToSettings = config => {
    const value = {
        name: '<name>',
        value: '<partial query string that will be inserted when the scope is selected>',
    };
    const edits = setProperty(config, ['search.scopes', -1], value, defaultFormattingOptions);
    return { edits, selectText: '<name>' };
};
const addSlackWebhook = config => {
    const value = {
        webhookURL: 'get webhook URL at https://YOUR-WORKSPACE-NAME.slack.com/apps/new/A0F7XDUAZ-incoming-webhooks',
    };
    const edits = setProperty(config, ['notifications.slack'], value, defaultFormattingOptions);
    return { edits, selectText: '""', cursorOffset: 1 };
};
export const settingsActions = [
    { id: 'sourcegraph.settings.searchScopes', label: 'Add search scope', run: addSearchScopeToSettings },
    { id: 'sourcegraph.settings.addSlackWebhook', label: 'Add Slack webhook', run: addSlackWebhook },
];
export const siteConfigActions = [
    { id: 'sourcegraph.site.githubDotCom', label: 'Add GitHub.com repositories', run: addGitHubDotCom },
    {
        id: 'sourcegraph.site.githubEnterprise',
        label: 'Add GitHub Enterprise repositories',
        run: addGitHubEnterprise,
    },
    { id: 'sourcegraph.site.addGitLab', label: 'Add GitLab projects', run: addGitLab },
    { id: 'sourcegraph.site.addBitbucketServer', label: 'Add Bitbucket Server repositories', run: addBitbucketServer },
    { id: 'sourcegraph.site.addAWSCodeCommit', label: 'Add AWS CodeCommit repositories', run: addAWSCodeCommit },
    { id: 'sourcegraph.site.otherRepository', label: 'Add other repository', run: addOtherRepository },
    {
        id: 'sourcegraph.site.addGSuiteOIDCAuthProvider',
        label: 'Add G Suite user auth',
        run: addGSuiteOIDCAuthProvider,
    },
    { id: 'sourcegraph.site.addSAMLAUthProvider', label: 'Add SAML user auth', run: addSAMLAuthProvider },
    { id: 'sourcegraph.site.addLicenseKey', label: 'Add license key', run: addLicenseKey },
];
export function getUpdateChannel(text) {
    const channel = getProperty(text, 'update.channel');
    return channel || 'release';
}
function getProperty(text, property) {
    try {
        const parsedConfig = parseJSON(text);
        return parsedConfig && parsedConfig[property] !== undefined ? parsedConfig[property] : null;
    }
    catch (err) {
        console.error(err);
        return null;
    }
}
//# sourceMappingURL=configHelpers.js.map