"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getParentSiteAbsolutePath = getParentSiteAbsolutePath;
exports.getParentSiteAbsoluteAndRelativePaths = getParentSiteAbsoluteAndRelativePaths;
exports.calculateBoilerplateFilePath = calculateBoilerplateFilePath;
exports.isUrl = isUrl;
exports.stripBaseUrl = stripBaseUrl;
const path_1 = __importDefault(require("path"));
const path_is_inside_1 = __importDefault(require("path-is-inside"));
const BOILERPLATE_FOLDER_NAME = '_markbind/boilerplates';
/**
 * Calculates the absolute path of of the immediate parent site of the specified filePath.
 * @param filePath The absolute file path to look up that is nested inside the root directory
 * @param root The base directory from which to terminate the look up
 * @param lookUp The set of urls representing the sites' base directories
 * @return String The immediate parent site's absolute path.
 * @throws If a non-absolute path or path outside the root is given
 */
function getParentSiteAbsolutePath(filePath, root, lookUp) {
    if (!path_1.default.isAbsolute(filePath)) {
        throw new Error(`Non-absolute path given to getParentSiteAbsolutePath: '${filePath}'`);
    }
    if (!(0, path_is_inside_1.default)(filePath, root)) {
        throw new Error(`Path given '${filePath}' is not in root '${root}'`);
    }
    function calculate(file) {
        if (file === root) {
            return file;
        }
        const parent = path_1.default.dirname(file);
        return lookUp.has(parent)
            ? parent
            : calculate(parent);
    }
    return calculate(filePath);
}
/**
 * Calculates the absolute and relative path of of the immediate parent site of the specified filePath.
 */
function getParentSiteAbsoluteAndRelativePaths(filePath, root, lookUp) {
    const absolute = getParentSiteAbsolutePath(filePath, root, lookUp);
    return {
        absolute,
        relative: path_1.default.relative(root, absolute),
    };
}
/**
 * Calculates a boilerplate's source file path at the immediate parent site of
 * the supplied file path.
 */
function calculateBoilerplateFilePath(pathInBoilerplates, asIfAt, config) {
    return path_1.default.resolve(getParentSiteAbsolutePath(asIfAt, config.rootPath, config.baseUrlMap), BOILERPLATE_FOLDER_NAME, pathInBoilerplates);
}
const isUrlRegex = /^(?:[a-z]+:)?\/\//i;
function isUrl(unknownPath) {
    return isUrlRegex.test(unknownPath);
}
function stripBaseUrl(src, baseUrl) {
    return src.startsWith(baseUrl)
        ? src.substring(baseUrl.length)
        : src;
}
//# sourceMappingURL=urlUtil.js.map