"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const catalogImport_service_1 = require("../services/catalogImport.service");
const parseArgs = (args) => {
    const options = {};
    const positional = [];
    for (let index = 0; index < args.length; index += 1) {
        const argument = args[index];
        if (!argument.startsWith('--')) {
            positional.push(argument);
            continue;
        }
        const [flag, inlineValue] = argument.split('=', 2);
        const nextValue = () => {
            if (inlineValue !== undefined)
                return inlineValue;
            const value = args[index + 1];
            if (!value || value.startsWith('--'))
                throw new Error(`${flag} requires a value.`);
            index += 1;
            return value;
        };
        if (flag === '--workbook')
            options.workbookPath = nextValue();
        else if (flag === '--dry-run')
            options.dryRun = true;
        else if (flag === '--no-resume')
            options.resume = false;
        else if (flag === '--assets') {
            const value = nextValue();
            if (value !== 'download') {
                throw new Error('--assets must be "download". Remote book images are not allowed.');
            }
            options.assetMode = 'download';
        }
        else if (flag === '--concurrency')
            options.concurrency = Number(nextValue());
        else if (flag === '--batch-size')
            options.batchSize = Number(nextValue());
        else if (flag === '--logs-dir')
            options.logsDir = nextValue();
        else if (flag === '--storage-dir')
            options.storageDir = nextValue();
        else if (flag === '--asset-public-base-url')
            options.assetPublicBaseUrl = nextValue();
        else if (flag === '--catalogue-map')
            options.catalogueMapPath = nextValue();
        else
            throw new Error(`Unknown option: ${flag}`);
    }
    options.workbookPath =
        options.workbookPath ?? positional[0] ?? process.env.CATALOG_WORKBOOK_PATH;
    if (!options.workbookPath) {
        throw new Error('Workbook path is required. Use --workbook <path> or set CATALOG_WORKBOOK_PATH.');
    }
    if (options.concurrency !== undefined && !Number.isFinite(options.concurrency)) {
        throw new Error('--concurrency must be a number.');
    }
    if (options.batchSize !== undefined && !Number.isFinite(options.batchSize)) {
        throw new Error('--batch-size must be a number.');
    }
    return options;
};
const main = async () => {
    const options = parseArgs(process.argv.slice(2));
    options.workbookPath = path_1.default.resolve(options.workbookPath);
    const summary = await (0, catalogImport_service_1.runCatalogImport)(options);
    process.stdout.write(`${JSON.stringify(summary, null, 2)}\n`);
};
main().catch((error) => {
    process.stderr.write(`${error instanceof Error ? error.stack ?? error.message : String(error)}\n`);
    process.exitCode = 1;
});
//# sourceMappingURL=importCatalog.js.map