// SPDX-License-Identifier: Apache-2.0
import { UnsupportedOperationError } from '../../business/errors/unsupported-operation-error.js';
import { Regex } from '../../business/utils/regex.js';
import { ConfigKeyFormatter } from './config-key-formatter.js';
export class Prefix {
    static DEFAULT_PREFIX = 'SOLO';
    constructor() {
        // Utility class
        throw new UnsupportedOperationError('Cannot instantiate utility class');
    }
    static add(key, prefix, formatter = ConfigKeyFormatter.instance()) {
        const normalizedKey = formatter.normalize(key);
        let finalPrefix = prefix ? formatter.normalize(prefix) : null;
        finalPrefix =
            finalPrefix && !finalPrefix.endsWith(formatter.separator) ? `${finalPrefix}${formatter.separator}` : finalPrefix;
        return finalPrefix && !normalizedKey.startsWith(finalPrefix) ? `${finalPrefix}${normalizedKey}` : normalizedKey;
    }
    static strip(key, prefix, formatter = ConfigKeyFormatter.instance()) {
        const normalizedKey = formatter.normalize(key);
        let finalPrefix = prefix ? formatter.normalize(prefix) : null;
        finalPrefix = finalPrefix?.endsWith(formatter.separator) ? finalPrefix : `${finalPrefix}${formatter.separator}`;
        return finalPrefix && normalizedKey.startsWith(finalPrefix)
            ? normalizedKey.replace(new RegExp(`^${Regex.escape(finalPrefix)}`), '')
            : normalizedKey;
    }
    static matcher(key, prefix, formatter = ConfigKeyFormatter.instance()) {
        if (!key) {
            return false;
        }
        let prefixFilter = prefix ? formatter.normalize(prefix) : null;
        if (prefixFilter && !prefixFilter.endsWith(formatter.separator)) {
            prefixFilter = [...prefixFilter, ...formatter.separator].join('');
        }
        return prefixFilter ? formatter.normalize(key)?.startsWith(prefixFilter) : true;
    }
}
//# sourceMappingURL=prefix.js.map