"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigFileTarget = void 0;
const codiac_domain_1 = require("@codiac.io/codiac-domain");
const type_safety_utils_1 = require("./type-safety-utils");
/**
 * Encodes and decodes the `targetFile` / `configName` field used throughout the config commands.
 *
 * This field is overloaded: it carries either a relative file path (e.g. `app-config.json`)
 * OR a special config type (e.g. `env`, `footprint`) encoded in square brackets (e.g. `[env]`).
 * The bracket encoding is the agreed convention for distinguishing the two cases in a single
 * string field — an acknowledged hack on the data model that this class exists to contain.
 *
 * All encode / decode / detect logic must go through this class so the convention is defined
 * exactly once and never re-inlined at call sites.
 */
class ConfigFileTarget {
    /** Returns the bracket-encoded representation of a special config type (e.g. `"[env]"`). */
    static encode(type) {
        return `[${type}]`;
    }
    /**
     * Returns the bracket-encoded type if `type` is defined, otherwise returns `targetFile`.
     * Returns `undefined` when both arguments are undefined.
     * Convenience overload for the common `configType ?? targetFile` pattern seen across commands.
     */
    static encodeOrFile(type, targetFile) {
        if (type != undefined)
            return ConfigFileTarget.encode(type);
        return targetFile;
    }
    /**
     * Same as `encodeOrFile` but throws when both arguments are undefined, for call sites that
     * require a resolved target (e.g. building an API request where the field is non-nullable).
     */
    static encodeOrFileOrThrow(type, targetFile) {
        const result = ConfigFileTarget.encodeOrFile(type, targetFile);
        if (result == undefined)
            throw new Error("A config file path (--file) or config type (--type) is required.");
        return result;
    }
    /**
     * Returns `true` when `value` is a bracket-encoded special config type rather than a file path.
     * Guards against `undefined` so call sites do not need a null check before calling.
     */
    static isEncoded(value) {
        return value != undefined && value.startsWith('[') && value.endsWith(']');
    }
    /**
     * Extracts and returns the `SpecialConfigTypesEnum` value from a bracket-encoded string
     * (e.g. `"[env]"` → `SpecialConfigTypesEnum.env`).
     * Returns `undefined` if the value is not bracket-encoded or does not match a known type.
     */
    static decode(value) {
        if (!ConfigFileTarget.isEncoded(value))
            return undefined;
        return type_safety_utils_1.TypeSafetyUtils.toEnum(codiac_domain_1.SpecialConfigTypesEnum, value.slice(1, -1));
    }
    /**
     * Returns the file path portion if `value` is a plain file path, or `undefined` if it is
     * a bracket-encoded type. Inverse of `decode`.
     */
    static toFile(value) {
        return ConfigFileTarget.isEncoded(value) ? undefined : value;
    }
}
exports.ConfigFileTarget = ConfigFileTarget;
//# sourceMappingURL=config-file-target.js.map