"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isUrl = void 0;
exports.validateLoadOption = validateLoadOption;
exports.validateTrackArg = validateTrackArg;
exports.validateLoadAndLocation = validateLoadAndLocation;
exports.validateAdapterType = validateAdapterType;
exports.validateAssemblies = validateAssemblies;
exports.validateTrackId = validateTrackId;
exports.createTargetDirectory = createTargetDirectory;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const isUrl = (loc) => loc?.match(/^https?:\/\//);
exports.isUrl = isUrl;
function validateLoadOption(load) {
    if (load && !['copy', 'symlink', 'move', 'inPlace'].includes(load)) {
        throw new Error('Error: --load must be one of: copy, symlink, move, inPlace');
    }
}
function validateTrackArg(track) {
    if (!track) {
        console.error('Missing 1 required arg:');
        console.error('track  Track file or URL');
        console.error('See more help with --help');
        process.exit(1);
    }
}
function validateLoadAndLocation(location, load) {
    if (isUrl(location) && load) {
        throw new Error('The --load flag is used for local files only, but a URL was provided');
    }
    else if (!isUrl(location) && !load) {
        throw new Error(`The --load flag should be used if a local file is used, example --load
        copy to copy the file into the config directory. Options for load are
        copy/move/symlink/inPlace (inPlace for no file operations)`);
    }
}
function validateAdapterType(adapterType) {
    if (adapterType === 'UNKNOWN') {
        throw new Error('Track type is not recognized');
    }
    if (adapterType === 'UNSUPPORTED') {
        throw new Error('Track type is not supported');
    }
}
function validateAssemblies(configContents, assemblyNames) {
    if (!configContents.assemblies?.length) {
        throw new Error('No assemblies found. Please add one before adding tracks');
    }
    if (configContents.assemblies.length > 1 && !assemblyNames) {
        throw new Error('Too many assemblies, cannot default to one. Please specify the assembly with the --assemblyNames flag');
    }
}
function validateTrackId(configContents, trackId, force, overwrite) {
    if (!configContents.tracks) {
        configContents.tracks = [];
    }
    const idx = configContents.tracks.findIndex(c => c.trackId === trackId);
    if (idx !== -1 && !force && !overwrite) {
        throw new Error(`Cannot add track with id ${trackId}, a track with that id already exists (use --force to override)`);
    }
    return idx;
}
function createTargetDirectory(configDir, subDir) {
    if (subDir) {
        const dir = path_1.default.join(configDir, subDir);
        if (!fs_1.default.existsSync(dir)) {
            fs_1.default.mkdirSync(dir);
        }
    }
}
