"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTrackedProjectKey = getTrackedProjectKey;
exports.resolveRepositoryHostProjectRef = resolveRepositoryHostProjectRef;
exports.resolveTicketTrackerProjectRef = resolveTicketTrackerProjectRef;
exports.resolveTrackedProjectRef = resolveTrackedProjectRef;
exports.resolveSupportedProviders = resolveSupportedProviders;
exports.getTrackedProjectStorageSegments = getTrackedProjectStorageSegments;
exports.getEventHookProjectStorageSegments = getEventHookProjectStorageSegments;
const catalog_1 = require("../i18n/catalog");
const tracked_project_1 = require("../core/tracked-project");
const registry_1 = require("./repository-host/registry");
const registry_2 = require("./ticket-tracker/registry");
function getTrackedProjectKey(ref) {
    return resolveTrackedProjectRef(ref).trackedProjectKey;
}
function resolveRepositoryHostProjectRef(repositoryHost, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const provider = (0, registry_1.requireRepositoryHostProvider)(repositoryHost.kind, locale);
    const projectRef = provider.normalizeProjectRef(repositoryHost.projectRef);
    provider.validateProjectRef(projectRef, locale);
    return { provider, projectRef };
}
function resolveTicketTrackerProjectRef(ticketTracker, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const provider = (0, registry_2.requireTicketTrackerProvider)(ticketTracker.kind, locale);
    const projectRef = provider.normalizeProjectRef(ticketTracker.projectRef);
    provider.validateProjectRef(projectRef, locale);
    return { provider, projectRef };
}
function resolveTrackedProjectRef(ref, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const repositoryHost = resolveRepositoryHostProjectRef(ref.repositoryHost, locale);
    const ticketTracker = resolveTicketTrackerProjectRef(ref.ticketTracker, locale);
    const normalizedRef = (0, tracked_project_1.toTrackedProjectRef)({
        repositoryHost: {
            kind: repositoryHost.provider.kind,
            projectRef: repositoryHost.projectRef,
        },
        ticketTracker: {
            kind: ticketTracker.provider.kind,
            projectRef: ticketTracker.projectRef,
        },
    });
    return {
        ref: normalizedRef,
        repositoryHostProvider: repositoryHost.provider,
        ticketTrackerProvider: ticketTracker.provider,
        trackedProjectKey: (0, tracked_project_1.serializeTrackedProjectKey)(normalizedRef),
    };
}
function resolveSupportedProviders(config) {
    const resolvedTrackedProject = resolveTrackedProjectRef((0, tracked_project_1.toTrackedProjectRef)(config), config.i18n.defaultLanguage);
    return {
        ...resolvedTrackedProject,
        config: applyTrackedProjectRef(config, resolvedTrackedProject.ref),
    };
}
function getTrackedProjectStorageSegments(ref) {
    const { ref: normalizedRef, repositoryHostProvider, ticketTrackerProvider, } = resolveTrackedProjectRef(ref);
    return [
        normalizedRef.repositoryHost.kind,
        ...repositoryHostProvider.toStorageSegments(normalizedRef.repositoryHost.projectRef),
        normalizedRef.ticketTracker.kind,
        ...ticketTrackerProvider.toStorageSegments(normalizedRef.ticketTracker.projectRef),
    ];
}
function getEventHookProjectStorageSegments(ref) {
    const { ref: normalizedRef, repositoryHostProvider, ticketTrackerProvider, } = resolveTrackedProjectRef(ref);
    const repositorySegments = [
        normalizedRef.repositoryHost.kind,
        ...repositoryHostProvider.toStorageSegments(normalizedRef.repositoryHost.projectRef),
    ];
    const trackerSegments = [
        normalizedRef.ticketTracker.kind,
        ...ticketTrackerProvider.toStorageSegments(normalizedRef.ticketTracker.projectRef),
    ];
    if (normalizedRef.repositoryHost.kind === normalizedRef.ticketTracker.kind &&
        normalizedRef.repositoryHost.projectRef ===
            normalizedRef.ticketTracker.projectRef) {
        return repositorySegments;
    }
    return ["repository", ...repositorySegments, "tracker", ...trackerSegments];
}
function applyTrackedProjectRef(config, ref) {
    return {
        ...config,
        repositoryHost: {
            ...config.repositoryHost,
            projectRef: ref.repositoryHost.projectRef,
        },
        ticketTracker: {
            ...config.ticketTracker,
            projectRef: ref.ticketTracker.projectRef,
        },
    };
}
