import { normalizeLanguageCode } from "@formbricks/i18n-utils/canonical";
import type { TSurvey as TInternalSurvey } from "@formbricks/types/surveys/types";

export type TV3SurveyResolverLanguage = {
  code: string;
  enabled: boolean;
  alias?: string | null;
};

export type TV3SurveyLanguage = {
  code: string;
  default: boolean;
  enabled: boolean;
  alias?: string | null;
};

type TV3SurveyLanguageQueryInput = string | string[];

type TResolveV3SurveyLanguageCodeResult =
  | { ok: true; code: string }
  | { ok: false; reason: "ambiguous" | "invalid" | "unknown"; message: string; normalizedCode?: string };

type TParseV3SurveyLanguageQueryResult = { ok: true; languages: string[] } | { ok: false; message: string };

// A region-qualified locale: 2-3 letter base language, optional 4-letter script, and a region that is
// either a 2-letter country or a 3-digit UN M.49 area. The 3-letter base + numeric region forms matter
// because canonical catalog codes include them (e.g. `fil-PH`, `bho-IN`, `eo-001`, `vo-001`); the old
// 2-letter-only pattern rejected those on write even though the picker/`createLanguage` accept them.
const V3_SURVEY_LOCALE_CODE_REGEX = /^[a-z]{2,3}(?:-[A-Z][a-z]{3})?-(?:[A-Z]{2}|\d{3})$/;
// A script-only tag with no region (e.g. `zh-Hans`). Kept as its own small regex so the tag check below is
// just "region-qualified OR script-only" — two simple patterns instead of one nested mega-regex.
const V3_SURVEY_SCRIPT_ONLY_REGEX = /^[a-z]{2,3}-[A-Z][a-z]{3}$/;

// Validate + case/separator-normalize a valid region/script-qualified BCP-47 tag (region-qualified OR
// script-only). This only recognises a valid tag — it does NOT canonicalize or complete it (`zh-Hans`
// stays `zh-Hans`, `zh-CN` stays `zh-CN`); canonicalization is `normalizeLanguageCode`'s job. Used for tag
// recognition and query dedup. Bare/underspecified/invalid input returns null.
export function normalizeV3SurveyLanguageTag(value: string): string | null {
  return normalizeV3SurveyLanguageCode(
    value,
    (code) => V3_SURVEY_LOCALE_CODE_REGEX.test(code) || V3_SURVEY_SCRIPT_ONLY_REGEX.test(code)
  );
}

export function normalizeV3SurveyLocaleCode(value: string): string | null {
  return normalizeV3SurveyLanguageCode(value, (code) => V3_SURVEY_LOCALE_CODE_REGEX.test(code));
}

function normalizeV3SurveyLanguageCode(
  value: string,
  isAcceptedTag: (code: string) => boolean
): string | null {
  const normalizedSeparators = value.trim().replaceAll("_", "-");

  try {
    const normalizedLanguage = Intl.getCanonicalLocales(normalizedSeparators)[0] ?? null;
    if (!normalizedLanguage || !isAcceptedTag(normalizedLanguage)) {
      return null;
    }

    return normalizedLanguage;
  } catch {
    return null;
  }
}

/**
 * A locale lookup costs a few microseconds, and a survey body repeats a handful of codes thousands of
 * times (every translatable field carries them, in the pre-pass and again in the normalizer), so the
 * results are remembered. Bounded: a body of distinct junk keys refills the cache rather than growing it.
 */
const LOCALE_LOOKUP_CACHE_MAX = 1024;
const LOCALE_LOOKUP_KEY_MAX_CHARS = 64;

function memoizeLocaleLookup(compute: (value: string) => string | null): (value: string) => string | null {
  const cache = new Map<string, string | null>();
  return (value) => {
    if (value.length > LOCALE_LOOKUP_KEY_MAX_CHARS) {
      return compute(value);
    }
    const cached = cache.get(value);
    if (cached !== undefined) {
      return cached;
    }
    const result = compute(value);
    if (cache.size >= LOCALE_LOOKUP_CACHE_MAX) {
      cache.clear();
    }
    cache.set(value, result);
    return result;
  };
}

const canonicalKey = memoizeLocaleLookup((value) => normalizeLanguageCode(value)?.toLowerCase() ?? null);

const newLanguageCode = memoizeLocaleLookup((value) => {
  const normalizedLocale = normalizeV3SurveyLocaleCode(value);
  return normalizedLocale ? (normalizeLanguageCode(normalizedLocale) ?? normalizedLocale) : null;
});

/** A survey's configured codes by lowercase form and by canonical key; where two match, the first listed. */
type TAllowedLanguageIndex = {
  codes: string[];
  byKey: Map<string, number>;
  byCanonical: Map<string, number>;
};

// Built once per list rather than once per translation key: a PATCH used to canonicalize every configured
// code again for every key in the body. Lists are the stored survey's and never change after the fact.
const allowedLanguageIndexes = new WeakMap<object, TAllowedLanguageIndex>();

function indexAllowedLanguageCodes(allowedLanguageCodes: readonly string[]): TAllowedLanguageIndex {
  const cached = allowedLanguageIndexes.get(allowedLanguageCodes);
  if (cached) {
    return cached;
  }

  const index: TAllowedLanguageIndex = {
    codes: [...allowedLanguageCodes],
    byKey: new Map(),
    byCanonical: new Map(),
  };
  index.codes.forEach((code, position) => {
    const key = code.toLowerCase();
    if (!index.byKey.has(key)) {
      index.byKey.set(key, position);
    }
    const canonical = canonicalKey(code);
    if (canonical && !index.byCanonical.has(canonical)) {
      index.byCanonical.set(canonical, position);
    }
  });
  allowedLanguageIndexes.set(allowedLanguageCodes, index);
  return index;
}

export function normalizeV3SurveyWriteLanguageCode(
  value: string,
  allowedLanguageCodes?: readonly string[]
): string | null {
  // PATCH: if the request resolves to a language ALREADY configured on the survey, write that survey's
  // STORED code as-is — its content i18n keys are keyed by exactly that code. Post-migration the stored
  // code is already canonical; a client still sending a legacy/script form matches by canonical
  // equivalence. Never rewrite it here (even for a canonical/script-complete input), or the write would
  // orphan the survey's existing content keys. This match runs BEFORE canonicalization so preserve wins.
  if (allowedLanguageCodes) {
    const { codes, byKey, byCanonical } = indexAllowedLanguageCodes(allowedLanguageCodes);
    const keyMatch = byKey.get(value.trim().toLowerCase());
    const requestedCanonical = canonicalKey(value);
    const canonicalMatch = requestedCanonical ? byCanonical.get(requestedCanonical) : undefined;
    // The first configured code that matches either way, as a scan in list order would find.
    const match = Math.min(keyMatch ?? Infinity, canonicalMatch ?? Infinity);
    if (match !== Infinity) {
      return codes[match];
    }
  }

  // CREATE (or a genuinely new language added via PATCH): accept a full locale and canonicalize it, so a
  // newly stored code is always canonical (`zh-CN` -> `zh-Hans-CN`) while legitimate non-default regions
  // are preserved (`de-AT` stays `de-AT`, `en-GB` stays `en-GB`). Bare/invalid codes stay rejected (null).
  return newLanguageCode(value);
}

function getLanguageSelectorKey(value: string): string {
  return normalizeV3SurveyLanguageTag(value)?.toLowerCase() ?? value.trim().toLowerCase();
}

export function parseV3SurveyLanguageQuery(
  value: TV3SurveyLanguageQueryInput
): TParseV3SurveyLanguageQueryResult {
  const requestedLanguages = (Array.isArray(value) ? value : [value])
    .flatMap((entry) => entry.split(","))
    .map((entry) => entry.trim());

  if (requestedLanguages.some((entry) => entry.length === 0)) {
    return {
      ok: false,
      message: "Language selector must contain valid comma-separated language selectors",
    };
  }

  const languages: string[] = [];
  const languageKeys = new Set<string>();

  for (const language of requestedLanguages) {
    const languageKey = getLanguageSelectorKey(language);

    if (!languageKeys.has(languageKey)) {
      languageKeys.add(languageKey);
      languages.push(language);
    }
  }

  return { ok: true, languages };
}

// Base language subtag (`de-AT` -> `de`, `zh-Hans-CN` -> `zh`), or null when there's no valid 2-3 letter
// base. Map-independent: only used to resolve/deduplicate bare language selectors.
function getLanguageBase(code: string): string | null {
  const base = code.trim().toLowerCase().split(/[-_]/)[0];
  return /^[a-z]{2,3}$/.test(base) ? base : null;
}

function isLanguageOnlySelector(code: string): boolean {
  return /^[a-z]{2,3}$/i.test(code);
}

function getV3SurveyLanguageAlias(alias: string | null | undefined): string | null {
  return alias?.trim() || null;
}

function createAmbiguousLanguageResult(
  requestedLanguage: string,
  normalizedCode: string | null,
  matchingCodes: string[]
): TResolveV3SurveyLanguageCodeResult {
  return {
    ok: false,
    reason: "ambiguous",
    normalizedCode: normalizedCode ?? requestedLanguage,
    message: `Language '${requestedLanguage}' is ambiguous for this survey. Matching languages: ${matchingCodes.join(
      ", "
    )}`,
  };
}

export function resolveV3SurveyLanguageCode(
  requestedLanguage: string,
  languages: TV3SurveyResolverLanguage[]
): TResolveV3SurveyLanguageCodeResult {
  const requestedLanguageValue = requestedLanguage.trim();
  const requestedLanguageKey = requestedLanguageValue.toLowerCase();
  const normalizedRequestedLanguage = normalizeV3SurveyLanguageTag(requestedLanguageValue);
  const requestedLanguageBase = getLanguageBase(requestedLanguageValue);

  // 1. Exact stored code or alias (case-insensitive) — always wins, even over the ambiguity heuristic below.
  let matches = languages.filter(
    (language) =>
      language.code.toLowerCase() === requestedLanguageKey ||
      getV3SurveyLanguageAlias(language.alias)?.toLowerCase() === requestedLanguageKey
  );

  // 2. No exact hit: a bare language selector matching more than one configured region/script is ambiguous
  // (`?lang=en` against both `en-US` and `en-GB`). Runs AFTER the exact/alias match so an exact stored code
  // or a configured alias (e.g. `en-US` aliased `en`) is never short-circuited by this heuristic.
  if (matches.length === 0 && isLanguageOnlySelector(requestedLanguageValue) && requestedLanguageBase) {
    const baseMatchCodes = Array.from(
      new Set(
        languages
          .filter((language) => getLanguageBase(language.code) === requestedLanguageBase)
          .map((language) => language.code)
      )
    );

    if (baseMatchCodes.length > 1) {
      return createAmbiguousLanguageResult(
        requestedLanguageValue,
        normalizedRequestedLanguage,
        baseMatchCodes
      );
    }
  }

  // 3. Canonical equivalence — a legacy/script/region/underscore selector resolves to the language whose
  // canonical BCP-47 form matches (`?lang=zh-Hans` or `?lang=zh-CN` -> a stored `zh-Hans-CN`; `?lang=DE_de`
  // -> `de-DE`). Runs only on zero exact matches, so an exact stored-code match always wins.
  if (matches.length === 0) {
    const requestedCanonical = canonicalKey(normalizedRequestedLanguage ?? requestedLanguageValue);
    if (requestedCanonical) {
      matches = languages.filter((language) => {
        const alias = getV3SurveyLanguageAlias(language.alias);
        return (
          canonicalKey(language.code) === requestedCanonical ||
          (alias ? canonicalKey(alias) === requestedCanonical : false)
        );
      });
    }
  }

  // 4. Base-language fallback for a bare selector (`?lang=pt` -> the single `pt-*` language).
  if (matches.length === 0 && isLanguageOnlySelector(requestedLanguageValue) && requestedLanguageBase) {
    matches = languages.filter((language) => getLanguageBase(language.code) === requestedLanguageBase);
  }

  // Return the survey's STORED code (canonical post-migration) so it lines up exactly with its content
  // i18n keys. Matching above already canonicalized the request; we only rewrite what we store, never
  // what we hand back for an existing survey.
  const matchCodes = Array.from(new Set(matches.map((language) => language.code)));

  if (matchCodes.length === 1) {
    return { ok: true, code: matchCodes[0] };
  }

  if (matchCodes.length > 1) {
    return createAmbiguousLanguageResult(requestedLanguageValue, normalizedRequestedLanguage, matchCodes);
  }

  if (!normalizedRequestedLanguage) {
    if (isLanguageOnlySelector(requestedLanguageValue)) {
      return {
        ok: false,
        reason: "unknown",
        normalizedCode: requestedLanguageValue,
        message: `Language '${requestedLanguageValue}' is not configured for this survey`,
      };
    }

    return {
      ok: false,
      reason: "invalid",
      message: `Language '${requestedLanguageValue}' is not a valid locale code or configured language alias`,
    };
  }

  return {
    ok: false,
    reason: "unknown",
    normalizedCode: normalizedRequestedLanguage,
    message: `Language '${normalizedRequestedLanguage}' is not configured for this survey`,
  };
}

export function getV3SurveyLanguages(
  survey: Pick<TInternalSurvey, "languages">,
  fallbackLanguage: string
): TV3SurveyLanguage[] {
  const languages = (survey.languages ?? []).map((surveyLanguage) => ({
    code: surveyLanguage.language.code,
    default: surveyLanguage.default,
    enabled: surveyLanguage.enabled,
    alias: getV3SurveyLanguageAlias(surveyLanguage.language.alias),
  }));

  if (languages.length === 0) {
    return [{ code: fallbackLanguage, default: true, enabled: true }];
  }

  return languages;
}

export function getV3SurveyResolverLanguages(
  survey: Pick<TInternalSurvey, "languages">,
  fallbackLanguage: string
): TV3SurveyResolverLanguage[] {
  const languages = (survey.languages ?? []).map((surveyLanguage) => ({
    code: surveyLanguage.language.code,
    enabled: surveyLanguage.enabled,
    alias: getV3SurveyLanguageAlias(surveyLanguage.language.alias),
  }));

  if (languages.length === 0) {
    return [{ code: fallbackLanguage, enabled: true }];
  }

  return languages;
}

export function getV3SurveyDefaultLanguage(
  survey: Pick<TInternalSurvey, "languages">,
  fallbackLanguage: string
): string {
  const defaultLanguageCode = survey.languages?.find((surveyLanguage) => surveyLanguage.default)?.language
    .code;

  return defaultLanguageCode ?? fallbackLanguage;
}
