"use strict";
/*
 * apidocts
 * https://apidocts.com
 * https://apidoc.app
 * Href Spa API Doc (TypeScript version)
 *
 * Author: Href Spa <hola@apidoc.app>
 * Copyright (c) 2025 Href SpA
 * Licensed under the MIT license.
 *
 * This project is a TypeScript refactor inspired by the original apidoc project.
 */
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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultOptions = void 0;
exports.process = process;
/**
 * All about options
 */
const fs = __importStar(require("fs"));
const lodash_1 = require("lodash");
const path = __importStar(require("path"));
const DEFAULT_DEST = 'doc';
const DEFAULT_SRC = ['./src'];
const DEFAULT_TEMPLATE = 'template';
const defaultOptions = {
    src: DEFAULT_SRC,
    dest: path.resolve(path.join(__dirname, '..', DEFAULT_DEST)) + path.sep,
    template: path.resolve(path.join(__dirname, '..', DEFAULT_TEMPLATE)) + path.sep,
    debug: false,
    silent: false,
    verbose: false,
    dryRun: false,
    colorize: true,
    markdown: true,
    config: '',
    apiprivate: false,
    encoding: 'utf8',
};
exports.defaultOptions = defaultOptions;
/**
 * Process options by merging with defaults, validating configuration paths,
 * and normalizing settings such as destinations, source paths, and line endings.
 *
 * @param options - User provided options
 * @param options.config - Path to the configuration file
 * @param options.src - Array of source paths to process. May be overridden by the configuration file settings.
 * @param options.dest - Output directory path
 * @param options.template - Template directory path
 * @param options.lineEnding - Line-ending format ('CRLF', 'CR', or 'LF')
 * @returns Normalized options object
 * @throws If the provided configuration file path is a directory instead of a file.
 */
function process(options) {
    // merge given options with defaults
    const mergedOptions = (0, lodash_1.defaults)({}, options, defaultOptions);
    // if a config file is given, read it to figure out input and output
    if (mergedOptions.config) {
        // make sure that we are provided a config file, not a directory
        if (fs.statSync(mergedOptions.config).isDirectory()) {
            throw new Error('[error] Invalid option: --config/-c must be a path to a file. Directory provided.');
        }
        const configPath = path.resolve(mergedOptions.config);
        const apidocConfig = require(configPath);
        // if dest is present in config file, set it in options, but only if it's the default value, as cli options should override config file options
        if (apidocConfig.output && mergedOptions.dest === defaultOptions.dest) {
            // keep a trailing slash
            mergedOptions.dest = path.resolve(path.join(apidocConfig.output, path.sep));
        }
        // do the same for input
        if (apidocConfig.input instanceof Array &&
            Array.isArray(mergedOptions.src) &&
            mergedOptions.src[0] === DEFAULT_SRC[0]) {
            // keep a trailing slash
            const input = apidocConfig.input.map((p) => path.resolve(p) + path.sep);
            mergedOptions.src = input;
        }
    }
    // add a trailing slash to output destination because it's always a folder
    mergedOptions.dest = path.join(mergedOptions.dest, path.sep);
    if (mergedOptions.template) {
        mergedOptions.template = path.join(mergedOptions.template, path.sep);
    }
    // Line-Ending option
    if (mergedOptions.lineEnding) {
        if (mergedOptions.lineEnding === 'CRLF') {
            // win32
            mergedOptions.lineEnding = '\r\n';
        }
        else if (mergedOptions.lineEnding === 'CR') {
            // darwin
            mergedOptions.lineEnding = '\r';
        }
        else {
            // linux
            mergedOptions.lineEnding = '\n';
        }
    }
    return mergedOptions;
}
// For CommonJS compatibility
module.exports = {
    process,
    defaultOptions,
};
//# sourceMappingURL=options.js.map