"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.remoteBranchExists = remoteBranchExists;
const http_1 = require("../../../util/http");
const result_1 = require("../../../util/result");
const common_1 = require("./common");
function headRef(repo, branchName) {
    return result_1.Result.wrap(common_1.githubApi.headJson(`/repos/${repo}/git/refs/heads/${branchName}`, {
        memCache: false,
    }))
        .transform(() => true)
        .catch((err) => {
        if (err instanceof http_1.HttpError && err.response?.statusCode === 404) {
            return result_1.Result.ok(false);
        }
        return result_1.Result.err(err);
    })
        .unwrapOrThrow();
}
async function remoteBranchExists(repo, branchName) {
    const refNested = `${branchName}/`;
    const isNested = await headRef(repo, refNested);
    if (isNested) {
        const message = `Trying to create a branch '${branchName}' while it's the part of nested branch`;
        throw new Error(message);
    }
    return headRef(repo, branchName);
}
//# sourceMappingURL=branch.js.map