// 页面数据相关的共享工具函数

export const MAX_TITLE_LENGTH = 200;
export const MAX_TAGS = 20;
export const MAX_TAG_LENGTH = 50;

/**
 * 清理文件名中的非法字符
 */
export function sanitizeFilename(name: string): string {
    return name.replace(/[<>:"/\\|?*]/g, '_').trim();
}

/**
 * 安全解析 JSON 格式的标签字符串
 */
export function safeParseTags(tags: string | null): string[] {
    if (!tags) return [];
    try {
        const parsed = JSON.parse(tags);
        return Array.isArray(parsed) ? parsed.filter((t): t is string => typeof t === 'string') : [];
    } catch {
        return [];
    }
}

/**
 * 标准化标签数组：去重、去空、截断
 */
export function normalizeTags(input: unknown): string[] {
    if (!Array.isArray(input)) return [];
    const unique = new Set<string>();
    for (const raw of input) {
        if (typeof raw !== 'string') continue;
        const normalized = raw.trim();
        if (!normalized || normalized.length > MAX_TAG_LENGTH) continue;
        unique.add(normalized);
        if (unique.size >= MAX_TAGS) break;
    }
    return Array.from(unique);
}
