"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleApiExportCommand = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const services_1 = require("../../api/services");
const ui_1 = require("../../utils/ui");
const arg_extractor_1 = require("../arg-extractor");
const shared_1 = require("./shared");
const defaultApiExportCommandDependencies = {
    exportApiProfile: services_1.exportApiProfile,
    initUI: ui_1.initUI,
    ok: ui_1.ok,
    warn: ui_1.warn,
    fail: ui_1.fail,
    getCwd: () => process.cwd(),
};
async function handleApiExportCommand(args, deps = defaultApiExportCommandDependencies) {
    await deps.initUI();
    const includeSecrets = (0, arg_extractor_1.hasAnyFlag)(args, ['--include-secrets']);
    const outExtracted = (0, arg_extractor_1.extractOption)(args, ['--out'], {
        allowDashValue: true,
        allowLongDashValue: true,
        knownFlags: ['--out', '--include-secrets'],
    });
    if (outExtracted.found && (outExtracted.missingValue || !outExtracted.value)) {
        console.log(deps.fail('Missing value for --out'));
        process.exit(1);
    }
    const syntax = (0, shared_1.collectUnexpectedApiArgs)(outExtracted.remainingArgs, {
        knownFlags: ['--include-secrets'],
        maxPositionals: 1,
    });
    if (syntax.errors.length > 0) {
        syntax.errors.forEach((errorMessage) => console.log(deps.fail(errorMessage)));
        process.exit(1);
    }
    const name = syntax.positionals[0];
    if (!name) {
        console.log(deps.fail('Profile name is required. Usage: ccs api export <name> [--out <file>]'));
        process.exit(1);
    }
    const result = deps.exportApiProfile(name, includeSecrets);
    if (!result.success || !result.bundle) {
        console.log(deps.fail(result.error || 'Failed to export profile'));
        process.exit(1);
    }
    const outputPath = path.resolve(deps.getCwd(), outExtracted.value || `${name}.ccs-profile.json`);
    fs.mkdirSync(path.dirname(outputPath), { recursive: true });
    fs.writeFileSync(outputPath, JSON.stringify(result.bundle, null, 2) + '\n', 'utf8');
    console.log(deps.ok(`Profile exported to: ${outputPath}`));
    if (result.redacted) {
        console.log(deps.warn('Token was redacted in export. Use --include-secrets to include it.'));
    }
    console.log('');
}
exports.handleApiExportCommand = handleApiExportCommand;
//# sourceMappingURL=export-command.js.map