export const ADVISORY_VISIBILITY_DEPRECATION = "visibility 'advisory' is deprecated; use 'private' instead.";
function output(data, json) {
    if (json) {
        process.stdout.write(JSON.stringify(data, null, 2) + "\n");
    }
    else if (typeof data === "string") {
        process.stdout.write(data + "\n");
    }
    else {
        process.stdout.write(JSON.stringify(data, null, 2) + "\n");
    }
}
export async function issuesCreate(opts, api) {
    const { mergeNeotomaToolingIssueLabels } = await import("../services/issues/github_client.js");
    const parsedLabels = opts.labels
        ? opts.labels
            .split(",")
            .map((l) => l.trim())
            .filter(Boolean)
        : undefined;
    const labels = mergeNeotomaToolingIssueLabels(parsedLabels);
    const visibility = opts.advisory === true ? "private" : (opts.visibility ?? "public");
    if (opts.advisory === true && !opts.json) {
        process.stderr.write(`${ADVISORY_VISIBILITY_DEPRECATION}\n`);
    }
    const { data, error } = await api.POST("/issues/submit", {
        body: {
            title: opts.title,
            body: opts.body,
            labels: labels.length > 0 ? labels : undefined,
            visibility,
            reporter_git_sha: opts.reporter_git_sha?.trim() || undefined,
            reporter_app_version: opts.reporter_app_version?.trim() || undefined,
            reporter_git_ref: opts.reporter_git_ref?.trim() || undefined,
            reporter_channel: opts.reporter_channel?.trim() || undefined,
            reporter_ci_run_id: opts.reporter_ci_run_id?.trim() || undefined,
        },
    });
    if (error) {
        process.stderr.write(`issues submit failed: ${JSON.stringify(error)}\n`);
        process.exitCode = 1;
        return;
    }
    const row = data;
    const issueNumber = row?.issue_number ?? 0;
    const githubUrl = row?.github_url ?? "";
    const entityId = row?.entity_id ?? "";
    const pushedToGithub = Boolean(row?.pushed_to_github);
    if (opts.json) {
        output({
            issue_number: issueNumber,
            github_url: githubUrl,
            entity_id: entityId,
            pushed_to_github: pushedToGithub,
            guest_access_token: row?.guest_access_token,
            github_mirror_guidance: row?.github_mirror_guidance ?? null,
        }, true);
    }
    else {
        if (pushedToGithub && issueNumber > 0) {
            process.stdout.write(`Created issue #${issueNumber}: ${githubUrl}\n`);
        }
        else {
            process.stdout.write(`Issue stored locally (GitHub push pending or private). Entity: ${entityId}\n`);
            if (row?.github_mirror_guidance) {
                process.stdout.write(`${row.github_mirror_guidance}\n`);
            }
        }
        if (row?.guest_access_token) {
            process.stdout.write("Guest access token returned. Treat it as a credential; pass it to issues status/message when needed.\n");
        }
    }
}
export async function issuesMessage(opts, api) {
    const entityId = opts.entity_id?.trim();
    const issueNumber = opts.issue_number;
    const hasEntity = typeof entityId === "string" && entityId.length > 0;
    const hasNumber = typeof issueNumber === "number" && Number.isFinite(issueNumber) && issueNumber > 0;
    if (!hasEntity && !hasNumber) {
        process.stderr.write("Error: provide a GitHub issue number as the command argument or --entity-id\n");
        process.exitCode = 1;
        return;
    }
    const { data, error } = await api.POST("/issues/add_message", {
        body: {
            ...(hasEntity ? { entity_id: entityId } : {}),
            ...(hasNumber ? { issue_number: issueNumber } : {}),
            body: opts.body,
            ...(opts.guest_access_token?.trim()
                ? { guest_access_token: opts.guest_access_token.trim() }
                : {}),
            reporter_git_sha: opts.reporter_git_sha?.trim() || undefined,
            reporter_app_version: opts.reporter_app_version?.trim() || undefined,
            reporter_git_ref: opts.reporter_git_ref?.trim() || undefined,
            reporter_channel: opts.reporter_channel?.trim() || undefined,
            reporter_ci_run_id: opts.reporter_ci_run_id?.trim() || undefined,
        },
    });
    if (error) {
        process.stderr.write(`issues message failed: ${JSON.stringify(error)}\n`);
        process.exitCode = 1;
        return;
    }
    const row = data;
    if (opts.json) {
        output({
            pushed_to_github: Boolean(row?.pushed_to_github),
            github_comment_id: row?.github_comment_id ?? null,
            message_entity_id: row?.message_entity_id,
            submitted_to_neotoma: Boolean(row?.submitted_to_neotoma),
            remote_submission_error: row?.remote_submission_error ?? null,
        }, true);
    }
    else {
        if (row?.pushed_to_github) {
            process.stdout.write(`Message added${hasNumber ? ` to issue #${issueNumber}` : ""}${row.github_comment_id ? ` (comment ${row.github_comment_id})` : ""}\n`);
        }
        else {
            process.stdout.write(`Message stored locally (GitHub push pending or private thread)\n`);
        }
        if (row?.remote_submission_error) {
            process.stderr.write(`Remote Neotoma append failed: ${row.remote_submission_error}\n`);
        }
    }
}
export async function issuesStatus(opts, api) {
    const entityId = opts.entity_id?.trim();
    const issueNumber = opts.issue_number;
    const hasEntity = typeof entityId === "string" && entityId.length > 0;
    const hasNumber = typeof issueNumber === "number" && Number.isFinite(issueNumber) && issueNumber > 0;
    if (!hasEntity && !hasNumber) {
        process.stderr.write("Error: provide --entity-id or --issue-number\n");
        process.exitCode = 1;
        return;
    }
    const { data, error } = await api.POST("/issues/status", {
        body: {
            ...(hasEntity ? { entity_id: entityId } : {}),
            ...(hasNumber ? { issue_number: issueNumber } : {}),
            skip_sync: opts.skip_sync === true,
            ...(opts.guest_access_token?.trim()
                ? { guest_access_token: opts.guest_access_token.trim() }
                : {}),
        },
    });
    if (error) {
        process.stderr.write(`issues status failed: ${JSON.stringify(error)}\n`);
        process.exitCode = 1;
        return;
    }
    if (opts.json) {
        output(data, true);
        return;
    }
    const row = data;
    process.stdout.write(`${row?.title ?? ""} [${row?.status ?? ""}]`);
    if (row?.issue_number)
        process.stdout.write(` #${row.issue_number}`);
    process.stdout.write("\n");
    if (row?.github_url)
        process.stdout.write(`${row.github_url}\n`);
    process.stdout.write(`synced=${Boolean(row?.synced)}\n\n`);
    for (const m of row?.messages ?? []) {
        process.stdout.write(`--- ${m.author} @ ${m.created_at}\n${m.body}\n\n`);
    }
}
export async function issuesList(opts, api) {
    const { listIssues } = await import("../services/issues/github_client.js");
    if (!opts.noSync) {
        try {
            const ghIssues = await listIssues({
                state: opts.status ?? "open",
                labels: opts.labels ? opts.labels.split(",").map((l) => l.trim()) : undefined,
                since: opts.since,
            });
            // Display directly from GitHub
            if (opts.json) {
                output(ghIssues.map((i) => ({
                    number: i.number,
                    title: i.title,
                    status: i.state,
                    labels: i.labels.map((l) => l.name),
                    author: i.user?.login ?? "unknown",
                    url: i.html_url,
                })), true);
            }
            else {
                if (ghIssues.length === 0) {
                    process.stdout.write("No issues found.\n");
                    return;
                }
                for (const issue of ghIssues) {
                    const labelStr = issue.labels.length
                        ? ` [${issue.labels.map((l) => l.name).join(", ")}]`
                        : "";
                    process.stdout.write(`#${issue.number} [${issue.state}] ${issue.title}${labelStr}\n`);
                }
            }
            return;
        }
        catch (err) {
            process.stderr.write(`Warning: sync from GitHub failed, showing local data: ${err.message}\n`);
        }
    }
    // Fallback: show from local store
    await api.POST("/store", {
        body: { entities: [] },
    });
    process.stdout.write(`Use --no-sync to view cached local data only.\n`);
}
export async function issuesSync(opts, api) {
    const labelArr = opts.labels
        ? opts.labels
            .split(",")
            .map((l) => l.trim())
            .filter(Boolean)
        : undefined;
    const { data, error } = await api.POST("/issues/sync", {
        body: {
            since: opts.since,
            state: opts.state ?? "all",
            labels: labelArr && labelArr.length > 0 ? labelArr : undefined,
        },
    });
    if (error) {
        process.stderr.write(`issues sync failed: ${JSON.stringify(error)}\n`);
        process.exitCode = 1;
        return;
    }
    const row = data;
    const issuesSynced = row?.issues_synced ?? 0;
    const messagesSynced = row?.messages_synced ?? 0;
    const errors = row?.errors ?? [];
    if (opts.json) {
        output({ issues_synced: issuesSynced, messages_synced: messagesSynced, errors }, true);
    }
    else {
        process.stdout.write(`Synced ${issuesSynced} issues, ${messagesSynced} messages.\n`);
        if (errors.length) {
            process.stderr.write(`Errors:\n${errors.map((e) => `  - ${e}`).join("\n")}\n`);
        }
    }
}
export async function issuesConfig(opts) {
    const { loadIssuesConfig, updateIssuesConfig } = await import("../services/issues/config.js");
    if (!opts.repo && !opts.mode && opts.authorAlias === undefined && !opts.clearAuthorAlias) {
        const config = await loadIssuesConfig();
        if (opts.json) {
            output(config, true);
        }
        else {
            process.stdout.write(`Issues configuration:\n`);
            process.stdout.write(`  GitHub auth: ${config.github_auth}\n`);
            process.stdout.write(`  Repo: ${config.repo}\n`);
            process.stdout.write(`  Reporting mode: ${config.reporting_mode}\n`);
            process.stdout.write(`  Author alias: ${config.author_alias ?? "not configured"}\n`);
            process.stdout.write(`  Sync staleness: ${config.sync_staleness_ms}ms\n`);
            process.stdout.write(`  Configured at: ${config.configured_at ?? "not configured"}\n`);
        }
        return;
    }
    const updates = {};
    if (opts.repo)
        updates.repo = opts.repo;
    if (opts.mode)
        updates.reporting_mode = opts.mode;
    if (opts.clearAuthorAlias) {
        updates.author_alias = null;
    }
    else if (opts.authorAlias !== undefined) {
        const alias = opts.authorAlias.trim();
        updates.author_alias = alias.length > 0 ? alias : null;
    }
    const updated = await updateIssuesConfig(updates);
    if (opts.json) {
        output(updated, true);
    }
    else {
        process.stdout.write(`Issues configuration updated.\n`);
    }
}
export async function issuesAuth(opts) {
    const { isGhInstalled, isGhAuthenticated, verifyGhAuth } = await import("../services/issues/gh_auth.js");
    const { updateIssuesConfig } = await import("../services/issues/config.js");
    const installed = await isGhInstalled();
    if (!installed) {
        const msg = "GitHub CLI (gh) is not installed. Install it with: brew install gh";
        if (opts.json) {
            output({ error: msg, gh_installed: false }, true);
        }
        else {
            process.stderr.write(`${msg}\n`);
        }
        return;
    }
    const authenticated = await isGhAuthenticated();
    if (!authenticated) {
        const msg = "GitHub CLI is not authenticated. Run: gh auth login";
        if (opts.json) {
            output({ error: msg, gh_installed: true, gh_authenticated: false }, true);
        }
        else {
            process.stderr.write(`${msg}\n`);
        }
        return;
    }
    const user = await verifyGhAuth();
    if (!user) {
        const msg = "GitHub CLI auth verification failed. Try: gh auth login --refresh";
        if (opts.json) {
            output({ error: msg, gh_installed: true, gh_authenticated: true, verified: false }, true);
        }
        else {
            process.stderr.write(`${msg}\n`);
        }
        return;
    }
    await updateIssuesConfig({ github_auth: "gh_cli" });
    if (opts.json) {
        output({ ok: true, login: user.login, github_auth: "gh_cli" }, true);
    }
    else {
        process.stdout.write(`Authenticated as ${user.login} via gh CLI.\n`);
        process.stdout.write(`Issues auth method set to: gh_cli\n`);
    }
}
//# sourceMappingURL=issues.js.map