const warnedInvalidEntries = new Set();
function warnInvalidAllowFromEntries(entries) {
    if (process.env.VITEST || process.env.NODE_ENV === "test") {
        return;
    }
    for (const entry of entries) {
        if (warnedInvalidEntries.has(entry)) {
            continue;
        }
        warnedInvalidEntries.add(entry);
        console.warn([
            "[telegram] Invalid allowFrom entry:",
            JSON.stringify(entry),
            "- allowFrom/groupAllowFrom authorization requires numeric Telegram sender IDs only.",
            'If you had "@username" entries, re-run onboarding (it resolves @username to IDs) or replace them manually.',
        ].join(" "));
    }
}
export const normalizeAllowFrom = (list) => {
    const entries = (list ?? []).map((value) => String(value).trim()).filter(Boolean);
    const hasWildcard = entries.includes("*");
    const normalized = entries
        .filter((value) => value !== "*")
        .map((value) => value.replace(/^(telegram|tg):/i, ""));
    const invalidEntries = normalized.filter((value) => !/^\d+$/.test(value));
    if (invalidEntries.length > 0) {
        warnInvalidAllowFromEntries([...new Set(invalidEntries)]);
    }
    const ids = normalized.filter((value) => /^\d+$/.test(value));
    return {
        entries: ids,
        hasWildcard,
        hasEntries: entries.length > 0,
        invalidEntries,
    };
};
export const normalizeAllowFromWithStore = (params) => {
    const combined = [...(params.allowFrom ?? []), ...(params.storeAllowFrom ?? [])]
        .map((value) => String(value).trim())
        .filter(Boolean);
    return normalizeAllowFrom(combined);
};
export const firstDefined = (...values) => {
    for (const value of values) {
        if (typeof value !== "undefined")
            return value;
    }
    return undefined;
};
export const isSenderAllowed = (params) => {
    const { allow, senderId } = params;
    if (!allow.hasEntries) {
        return true;
    }
    if (allow.hasWildcard) {
        return true;
    }
    if (senderId && allow.entries.includes(senderId)) {
        return true;
    }
    return false;
};
export const resolveSenderAllowMatch = (params) => {
    const { allow, senderId } = params;
    if (allow.hasWildcard) {
        return { allowed: true, matchKey: "*", matchSource: "wildcard" };
    }
    if (!allow.hasEntries)
        return { allowed: false };
    if (senderId && allow.entries.includes(senderId)) {
        return { allowed: true, matchKey: senderId, matchSource: "id" };
    }
    return { allowed: false };
};
