import { normalizeStringArray } from "./normalize-string-array";

/**
 * Parse stored settings value for blocked company keywords.
 * Accepts JSON string array (normal) or legacy plain comma/newline-separated text.
 */
export function resolveBlockedCompanyKeywordsFromStoredString(
  raw: string | undefined,
): string[] {
  if (!raw?.trim()) return [];
  try {
    const parsed: unknown = JSON.parse(raw);
    if (Array.isArray(parsed)) {
      return normalizeStringArray(
        parsed.filter((item): item is string => typeof item === "string"),
      );
    }
  } catch {
    // Legacy or corrupted JSON: treat as token list
  }
  return normalizeStringArray(
    raw
      .split(/[\n,]/g)
      .map((segment) => segment.trim())
      .filter(Boolean),
  );
}
