"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * 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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TeamConfigurationPreferenceStore = exports.TeamConfigurationPreferenceStoreFactory = void 0;
const GraphClient_1 = require("@atomist/automation-client/lib/spi/graph/GraphClient");
const AbstractPreferenceStore_1 = require("./AbstractPreferenceStore");
/**
 * Factory to create a new TeamConfigurationPreferenceStore instance
 */
const TeamConfigurationPreferenceStoreFactory = ctx => new TeamConfigurationPreferenceStore(ctx);
exports.TeamConfigurationPreferenceStoreFactory = TeamConfigurationPreferenceStoreFactory;
/**
 * PreferenceStore implementation that stores preferences in the backend GraphQL store.
 */
class TeamConfigurationPreferenceStore extends AbstractPreferenceStore_1.AbstractPreferenceStore {
    constructor(context) {
        super(context);
        this.context = context;
    }
    async doGet(name, namespace) {
        const result = await this.context.graphClient.query({
            name: "TeamConfigurationByNamespace",
            variables: {
                namespace: normalizeNamespace(namespace),
            },
            options: GraphClient_1.QueryNoCacheOptions,
        });
        const teamConfiguration = (result.TeamConfiguration || []).find(t => t.name === name);
        if (!!teamConfiguration) {
            return {
                name,
                namespace,
                value: teamConfiguration.value,
                ttl: undefined,
            };
        }
        return undefined;
    }
    async doPut(pref) {
        await this.context.graphClient.mutate({
            name: "SetTeamConfiguration",
            variables: {
                name: pref.name,
                namespace: normalizeNamespace(pref.namespace),
                value: pref.value,
                ttl: typeof pref.ttl === "number" ? Math.floor(pref.ttl / 1000) : undefined,
            },
            options: GraphClient_1.MutationNoCacheOptions,
        });
    }
    async doList(namespace) {
        const result = await this.context.graphClient.query({
            name: "TeamConfigurationByNamespace",
            variables: {
                namespace: normalizeNamespace(namespace),
            },
            options: GraphClient_1.QueryNoCacheOptions,
        });
        if (!!result.TeamConfiguration) {
            return result.TeamConfiguration.map(t => ({
                name: t.name,
                namespace: t.namespace,
                value: t.value,
                ttl: undefined,
            }));
        }
        return [];
    }
    async doDelete(name, namespace) {
        await this.context.graphClient.mutate({
            name: "DeleteTeamConfiguration",
            variables: {
                name,
                namespace: normalizeNamespace(namespace),
            },
            options: GraphClient_1.MutationNoCacheOptions,
        });
    }
}
exports.TeamConfigurationPreferenceStore = TeamConfigurationPreferenceStore;
function normalizeNamespace(namespace) {
    if (!namespace || namespace.length === 0) {
        return "@atomist.global";
    }
    // Backend doesn't allow / inside a namespace
    return namespace.replace(/\//g, ".");
}
//# sourceMappingURL=TeamConfigurationPreferenceStore.js.map