export var PullRequestProvider;
(function (PullRequestProvider) {
    PullRequestProvider["GITHUB"] = "github";
    PullRequestProvider["GITLAB"] = "gitlab";
})(PullRequestProvider || (PullRequestProvider = {}));
export var PullRequestSource;
(function (PullRequestSource) {
    PullRequestSource["CUSTOM_METRIC"] = "custom_metric";
    PullRequestSource["CUSTOM_DIMENSION"] = "custom_dimension";
    PullRequestSource["SQL_RUNNER"] = "sql_runner";
    PullRequestSource["SOURCE_EDITOR"] = "source_editor";
    PullRequestSource["AI_AGENT"] = "ai_agent";
})(PullRequestSource || (PullRequestSource = {}));
export var PullRequestState;
(function (PullRequestState) {
    PullRequestState["OPEN"] = "open";
    PullRequestState["CLOSED"] = "closed";
    PullRequestState["MERGED"] = "merged";
})(PullRequestState || (PullRequestState = {}));
const PREVIEW_PROJECT_UUID = '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}';
/**
 * Find the Lightdash preview-environment URL inside a pull request's comments.
 *
 * When the AI write-back opens a PR against a dbt repo, that repo's CI runs
 * `lightdash preview`, which publishes a PREVIEW-type project and posts its URL
 * (`{siteUrl}/projects/{previewProjectUuid}/...`) as a PR comment. Only URLs on
 * this instance's own host are considered, so a link to github.com or an
 * unrelated site is never mistaken for a preview. Comments are scanned
 * newest-first, so a re-published preview wins. Returns null when none match.
 */
export const extractPreviewUrlFromComments = (commentBodies, siteUrl) => {
    let host;
    try {
        host = new URL(siteUrl).host;
    }
    catch {
        return null;
    }
    const escapedHost = host.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const previewUrlRegex = new RegExp(`https?://${escapedHost}/projects/${PREVIEW_PROJECT_UUID}(?:/[\\w-]+)*`, 'i');
    for (let i = commentBodies.length - 1; i >= 0; i -= 1) {
        const match = commentBodies[i].match(previewUrlRegex);
        if (match) {
            return match[0];
        }
    }
    return null;
};
export const extractPreviewProjectUuidFromUrl = (previewUrl, siteUrl) => {
    try {
        const preview = new URL(previewUrl);
        const site = new URL(siteUrl);
        if (preview.host !== site.host) {
            return null;
        }
        const match = preview.pathname.match(new RegExp(`^/projects/(${PREVIEW_PROJECT_UUID})(?:/|$)`, 'i'));
        return match?.[1] ?? null;
    }
    catch {
        return null;
    }
};
//# sourceMappingURL=gitIntegration.js.map