import WarpError from "../errors/WarpError.js";
const NUL_BYTE = '\0';
export default class TreeEntryPath {
    value;
    constructor(value) {
        assertTreeEntryPath(value);
        this.value = value;
        Object.freeze(this);
    }
    withoutTrailingSlash() {
        if (!this.value.endsWith('/')) {
            return this;
        }
        let end = this.value.length;
        while (end > 0 && this.value[end - 1] === '/') {
            end -= 1;
        }
        return new TreeEntryPath(this.value.slice(0, end));
    }
}
function assertTreeEntryPath(value) {
    assertTreeEntryPathContent(value);
    assertTreeEntryPathBoundaries(value);
}
function assertTreeEntryPathContent(value) {
    if (value.trim().length === 0) {
        throwTreeEntryPathError('Tree entry path must be non-empty');
    }
    if (value.includes(NUL_BYTE)) {
        throwTreeEntryPathError('Tree entry path must not contain NUL bytes');
    }
}
function assertTreeEntryPathBoundaries(value) {
    if (value.trim() !== value) {
        throwTreeEntryPathError('Tree entry path must not have leading or trailing whitespace');
    }
    if (value.startsWith('/')) {
        throwTreeEntryPathError('Tree entry path must be relative');
    }
    if (value.startsWith(':')) {
        throwTreeEntryPathError('Tree entry path must not use Git pathspec magic');
    }
}
function throwTreeEntryPathError(message) {
    throw new WarpError(message, 'E_TREE_ENTRY_PATH');
}
