"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.openIssueCreationLink = exports.maybePrintIssueCreationPrompt = exports.shouldRecommendIssue = void 0;
const chalk_1 = __importDefault(require("chalk"));
const open_1 = __importDefault(require("open"));
const querystring_1 = require("querystring");
const path_1 = require("./path");
const repoSpecifier = /^([\w.-]+)\/([\w.-]+)$/;
const githubURL = /github.com(:|\/)([\w.-]+\/[\w.-]+?)(.git|\/.*)?$/;
function parseRepoString(repository) {
    if (repository.startsWith("github:")) {
        repository = repository.replace(/^github:/, "");
    }
    const urlMatch = repository.match(githubURL);
    if (urlMatch) {
        repository = urlMatch[2];
    }
    const specMatch = repository.match(repoSpecifier);
    if (!specMatch) {
        return null;
    }
    const [, org, repo] = specMatch;
    return { org, repo, provider: "GitHub" };
}
function getPackageVCSDetails(packageDetails) {
    const repository = require(path_1.resolve(path_1.join(packageDetails.path, "package.json")))
        .repository;
    if (!repository) {
        return null;
    }
    if (typeof repository === "string") {
        return parseRepoString(repository);
    }
    else if (typeof repository === "object" &&
        typeof repository.url === "string") {
        return parseRepoString(repository.url);
    }
}
function shouldRecommendIssue(vcsDetails) {
    if (!vcsDetails) {
        return true;
    }
    const { repo, org } = vcsDetails;
    if (repo === "DefinitelyTyped" && org === "DefinitelyTyped") {
        return false;
    }
    return true;
}
exports.shouldRecommendIssue = shouldRecommendIssue;
function maybePrintIssueCreationPrompt(packageDetails, packageManager) {
    const vcs = getPackageVCSDetails(packageDetails);
    if (vcs && shouldRecommendIssue(vcs)) {
        console.log(`💡 ${chalk_1.default.bold(packageDetails.name)} is on ${vcs.provider}! To draft an issue based on your patch run

    ${packageManager === "yarn" ? "yarn" : "npx"} patch-package ${packageDetails.pathSpecifier} --create-issue
`);
    }
}
exports.maybePrintIssueCreationPrompt = maybePrintIssueCreationPrompt;
function openIssueCreationLink({ packageDetails, patchFileContents, packageVersion, }) {
    const vcs = getPackageVCSDetails(packageDetails);
    if (!vcs) {
        console.error(`Error: Couldn't find VCS details for ${packageDetails.pathSpecifier}`);
        process.exit(1);
    }
    // trim off trailing newline since we add an extra one in the markdown block
    if (patchFileContents.endsWith("\n")) {
        patchFileContents = patchFileContents.slice(0, -1);
    }
    open_1.default(`https://github.com/${vcs.org}/${vcs.repo}/issues/new?${querystring_1.stringify({
        title: "",
        body: `Hi! 👋 
      
Firstly, thanks for your work on this project! 🙂

Today I used [patch-package](https://github.com/ds300/patch-package) to patch \`${packageDetails.name}@${packageVersion}\` for the project I'm working on.

<!-- 🔺️🔺️🔺️ PLEASE REPLACE THIS BLOCK with a description of your problem, and any other relevant context 🔺️🔺️🔺️ -->

Here is the diff that solved my problem:

\`\`\`diff
${patchFileContents}
\`\`\`

<em>This issue body was [partially generated by patch-package](https://github.com/ds300/patch-package/issues/296).</em>
`,
    })}`);
}
exports.openIssueCreationLink = openIssueCreationLink;
//# sourceMappingURL=data:application/json;base64,[encoded data, 7832 characters]