export const DEFAULT_KNOWLEDGE_SPACE_ID = 'default';
export const HOME_ASSISTANT_KNOWLEDGE_SPACE_PREFIX = 'homeassistant:';
export const PROJECT_KNOWLEDGE_SPACE_PREFIX = 'project:';
export function normalizeKnowledgeSpaceId(value) {
    const trimmed = typeof value === 'string' ? value.trim() : '';
    return trimmed.length > 0 ? trimmed : DEFAULT_KNOWLEDGE_SPACE_ID;
}
export function homeAssistantKnowledgeSpaceId(installationId) {
    const normalized = normalizeSpaceComponent(installationId ?? 'default');
    return `${HOME_ASSISTANT_KNOWLEDGE_SPACE_PREFIX}${normalized}`;
}
export function normalizeHomeAssistantInstallationId(value) {
    return normalizeSpaceComponent(value ?? 'default');
}
export function isHomeAssistantKnowledgeSpace(spaceId) {
    return normalizeKnowledgeSpaceId(spaceId).startsWith(HOME_ASSISTANT_KNOWLEDGE_SPACE_PREFIX);
}
export function projectKnowledgeSpaceId(projectId) {
    return `${PROJECT_KNOWLEDGE_SPACE_PREFIX}${normalizeProjectId(projectId ?? 'default')}`;
}
export function normalizeProjectId(value) {
    return normalizeSpaceComponent(value ?? 'default');
}
export function isProjectKnowledgeSpace(spaceId) {
    return normalizeKnowledgeSpaceId(spaceId).startsWith(PROJECT_KNOWLEDGE_SPACE_PREFIX);
}
export function knowledgeSpaceMetadata(spaceId, metadata = {}) {
    const normalized = normalizeKnowledgeSpaceId(spaceId);
    return {
        ...metadata,
        knowledgeSpaceId: normalized,
        namespace: normalized,
    };
}
export function withKnowledgeSpace(input, spaceId) {
    return {
        ...input,
        metadata: knowledgeSpaceMetadata(spaceId, input.metadata ?? {}),
    };
}
export function getKnowledgeSpaceId(input) {
    return getExplicitKnowledgeSpaceId(input) ?? DEFAULT_KNOWLEDGE_SPACE_ID;
}
export function getExplicitKnowledgeSpaceId(input) {
    const metadata = readMetadata(input);
    const value = readString(metadata.knowledgeSpaceId)
        ?? readString(metadata.spaceId)
        ?? readString(metadata.namespace);
    return value ? normalizeKnowledgeSpaceId(value) : null;
}
export function isInKnowledgeSpace(input, spaceId) {
    return getKnowledgeSpaceId(input) === normalizeKnowledgeSpaceId(spaceId);
}
export function resolveKnowledgeSpaceScope(input = {}) {
    if (input.includeAllSpaces === true)
        return null;
    return normalizeKnowledgeSpaceId(input.knowledgeSpaceId);
}
export function isInKnowledgeSpaceScope(input, scope = {}) {
    const scopedSpaceId = resolveKnowledgeSpaceScope(scope);
    if (scopedSpaceId === null)
        return true;
    return getKnowledgeSpaceId(input) === scopedSpaceId;
}
export function ensureKnowledgeSpaceMetadata(metadata = {}, fallbackSpaceId = DEFAULT_KNOWLEDGE_SPACE_ID) {
    return getExplicitKnowledgeSpaceId({ metadata })
        ? metadata
        : knowledgeSpaceMetadata(fallbackSpaceId, metadata);
}
export function normalizeSpaceComponent(value) {
    const normalized = (typeof value === 'string' ? value : '')
        .trim()
        .toLowerCase()
        .replace(/[^a-z0-9_.:-]+/g, '-')
        .replace(/^-+|-+$/g, '');
    return normalized.length > 0 ? normalized : 'default';
}
function readMetadata(input) {
    if (!input || typeof input !== 'object')
        return {};
    const candidate = input.metadata;
    return candidate && typeof candidate === 'object' && !Array.isArray(candidate)
        ? candidate
        : {};
}
function readString(value) {
    return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
}
