package com.tzld.piaoquan.api.util;

import java.text.Normalizer;
import java.util.regex.Pattern;

/**
 * 标题归一化：用于「视频标题 == 需求种子标题」的模糊比对，
 * 规避脏数据中的 emoji 差异、空白差异、全/半角差异。
 */
public final class TitleNormalizer {

    private TitleNormalizer() {
    }

    // 覆盖常见 emoji 区间（符号、表情、补充符号、各类杂项符号）
    private static final Pattern EMOJI = Pattern.compile(
            "[\\p{So}\\p{Cn}]" +
                    "|[\uD83C-\uDBFF][\uDC00-\uDFFF]" +
                    "|[\u2600-\u27BF]" +
                    "|[\uFE00-\uFE0F]" +
                    "|[\u200D]"
    );

    private static final Pattern WHITESPACE = Pattern.compile("\\s+");

    public static String normalize(String s) {
        if (s == null || s.isEmpty()) {
            return "";
        }
        String n = Normalizer.normalize(s, Normalizer.Form.NFKC);
        n = EMOJI.matcher(n).replaceAll("");
        n = WHITESPACE.matcher(n).replaceAll("");
        return n;
    }

    public static boolean isSelfTitle(String title, String demandContentTitle) {
        if (title == null || demandContentTitle == null) {
            return false;
        }
        String a = normalize(title);
        String b = normalize(demandContentTitle);
        return !a.isEmpty() && a.equals(b);
    }
}
