/*
 * Copyright 2020 The Kubernetes Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
function settings(registrar) {
    const settings = ['prompt', '_for_testing_'];
    /** Remove all user setting overrides */
    registrar.listen('/kuiconfig/reset', () => __awaiter(this, void 0, void 0, function* () {
        const { reset } = yield import('./components/Client/UserSettings');
        yield reset();
        return true;
    }));
    settings.forEach(setting => {
        /** Read out a given user setting override */
        registrar.listen(`/kuiconfig/get/${setting}`, () => __awaiter(this, void 0, void 0, function* () {
            const { get } = yield import('./components/Client/UserSettings');
            const { i18n } = yield import('@kui-shell/core/mdist/api/i18n');
            const strings = i18n('plugin-client-common/user-settings');
            const value = yield get(setting);
            if (!value) {
                const error = new Error(strings('You have not specified a value for this setting'));
                error.code = 404;
                throw error;
            }
            else if (typeof value === 'string' || typeof value === 'boolean' || typeof value === 'number') {
                return value;
            }
            else {
                return value.toString();
            }
        }));
        /** Update a given user setting override */
        registrar.listen(`/kuiconfig/set/${setting}`, (args) => __awaiter(this, void 0, void 0, function* () {
            const { set } = yield import('./components/Client/UserSettings');
            yield set(setting, args.argvNoOptions[3]);
            return true;
        }));
        /** Does the given setting have a value? */
        registrar.listen(`/kuiconfig/is/set/${setting}`, () => __awaiter(this, void 0, void 0, function* () {
            const { get } = yield import('./components/Client/UserSettings');
            const value = yield get(setting);
            return value !== undefined;
        }));
        /** Does the given setting not have a value? */
        registrar.listen(`/kuiconfig/not/set/${setting}`, () => __awaiter(this, void 0, void 0, function* () {
            const { get } = yield import('./components/Client/UserSettings');
            const value = yield get(setting);
            return value === undefined;
        }));
        /** Remove a given user setting override */
        registrar.listen(`/kuiconfig/unset/${setting}`, () => __awaiter(this, void 0, void 0, function* () {
            const { unset } = yield import('./components/Client/UserSettings');
            yield unset(setting);
            return true;
        }));
    });
}
export default function registerPluginClientCommonControllers(registrar) {
    registrar.listen('/confirm', args => import('./controller/confirm').then(_ => _.default(args)), {
        incognito: ['popup']
    });
    registrar.listen('/split', args => import('./controller/split').then(_ => _.default(args)), {
        flags: { boolean: ['inverse'], alias: { split: ['s'] } }
    });
    registrar.listen('/split-debug', args => import('./controller/split').then(_ => _.debug(args)));
    registrar.listen('/split-count', ({ tab }) => tab.splitCount());
    registrar.listen('/is-split', ({ tab }) => tab.hasSideBySideTerminals());
    const flags = {
        boolean: ['edit', 'header', 'preview', 'readonly', 'replace', 'simple']
    };
    const commentaryFlags = { outputOnly: true, flags };
    const hashFlags = { outputOnly: true, flags, noCoreRedirect: true };
    const commentaryDelegateFlags = { outputOnly: true, needsUI: true };
    registrar.listen('/alert', args => import('./controller/alert').then(_ => _.default(args)));
    registrar.listen('/card', args => import('./controller/card').then(_ => _.default(args)));
    registrar.listen('/commentary', args => import('./controller/commentary').then(_ => _.addComment(args)), commentaryFlags);
    registrar.listen('/#', args => import('./controller/commentary').then(_ => _.addComment(args)), hashFlags);
    registrar.listen('/commentary/delegate', args => import('./controller/commentary').then(_ => _.delegate(args)), commentaryDelegateFlags);
    settings(registrar);
}
//# sourceMappingURL=plugin.js.map