"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveInitTargetCwd = resolveInitTargetCwd;
exports.validateTargetedInitOptions = validateTargetedInitOptions;
exports.isTargetedGiteaInit = isTargetedGiteaInit;
exports.validateTargetedGiteaProjectRefs = validateTargetedGiteaProjectRefs;
exports.validateTargetedGiteaServiceOptions = validateTargetedGiteaServiceOptions;
const node_path_1 = __importDefault(require("node:path"));
const repository_setup_1 = require("../../git/repository-setup");
const provider_resolution_1 = require("../../platform/provider-resolution");
async function resolveInitTargetCwd(cwd, options) {
    const targetCwd = node_path_1.default.resolve(cwd, options.cwd ?? ".");
    if (!isTargetedGiteaInit(options)) {
        return targetCwd;
    }
    return (await (0, repository_setup_1.discoverContainingGitRoot)(targetCwd)) ?? targetCwd;
}
function validateTargetedInitOptions(options) {
    if (!isTargetedGiteaInit(options)) {
        return;
    }
    validateTargetedGiteaServiceOptions(options);
}
function isTargetedGiteaInit(options) {
    return options.cwd !== undefined || options.createRepo === true;
}
function validateTargetedGiteaProjectRefs(options, locale) {
    if (!isTargetedGiteaInit(options)) {
        return;
    }
    if (options.repositoryHostProjectRef === undefined ||
        options.ticketTrackerProjectRef === undefined) {
        return;
    }
    const repositoryProjectRef = (0, provider_resolution_1.resolveRepositoryHostProjectRef)({
        kind: "gitea",
        projectRef: options.repositoryHostProjectRef,
    }, locale).projectRef;
    const trackerProjectRef = (0, provider_resolution_1.resolveTicketTrackerProjectRef)({
        kind: "gitea",
        projectRef: options.ticketTrackerProjectRef,
    }, locale).projectRef;
    if (repositoryProjectRef !== trackerProjectRef) {
        throw new Error("Gitea init v1 requires the tracker project to match the repository project.");
    }
}
function validateTargetedGiteaServiceOptions(options) {
    if (options.repositoryHostKind !== "gitea") {
        throw new Error("alpaca init --cwd and --create-repo require --repo-provider gitea.");
    }
    if (options.ticketTrackerKind !== undefined &&
        options.ticketTrackerKind !== "gitea") {
        throw new Error("Gitea init v1 requires the tracker provider to match the repository provider.");
    }
}
