export const MARQUEE_SETTINGS_KEY = "marquee_settings";

export type MarqueeLocaleSettings = {
  row1: string;
  row2: string;
  row3: string;
  row4: string;
};

export type MarqueeSettings = {
  locales: {
    ar: MarqueeLocaleSettings;
    en: MarqueeLocaleSettings;
    de: MarqueeLocaleSettings;
  };
};

const DEFAULT_ROW_1 = [
  "Next.js",
  "React",
  "WordPress",
  "Docker",
  "Technical SEO",
  "Tailwind CSS",
  "Google Analytics",
  "Framer Motion",
  "Cloudflare",
  "Performance",
].join("\n");

const DEFAULT_ROW_2 = [
  "TypeScript",
  "Node.js",
  "WooCommerce",
  "Schema Markup",
  "DevOps",
  "Responsive Design",
  "Core Web Vitals",
  "Plausible Analytics",
  "Design Systems",
  "Server Infrastructure",
].join("\n");

const DEFAULT_ROW_3 = [
  "JavaScript",
  "REST APIs",
  "Git",
  "Linux",
  "Search Console",
  "Conversion Tracking",
  "UI Engineering",
  "Accessibility",
  "Component Architecture",
  "Web Performance",
].join("\n");

const DEFAULT_ROW_4 = [
  "Frontend Strategy",
  "CMS Integration",
  "Motion Design",
  "Content Architecture",
  "E-Commerce",
  "SEO Audits",
  "Deployments",
  "Scalable UI",
  "Analytics Setup",
  "Product Pages",
].join("\n");

function buildDefaultLocaleSettings(): MarqueeLocaleSettings {
  return {
    row1: DEFAULT_ROW_1,
    row2: DEFAULT_ROW_2,
    row3: DEFAULT_ROW_3,
    row4: DEFAULT_ROW_4,
  };
}

export function buildDefaultMarqueeSettings(): MarqueeSettings {
  return {
    locales: {
      ar: buildDefaultLocaleSettings(),
      en: buildDefaultLocaleSettings(),
      de: buildDefaultLocaleSettings(),
    },
  };
}

function normalizeLocaleSettings(value: unknown, defaults: MarqueeLocaleSettings): MarqueeLocaleSettings {
  const candidate = value && typeof value === "object" ? (value as Record<string, unknown>) : {};

  return {
    row1: typeof candidate.row1 === "string" && candidate.row1.trim() ? candidate.row1.trim() : defaults.row1,
    row2: typeof candidate.row2 === "string" && candidate.row2.trim() ? candidate.row2.trim() : defaults.row2,
    row3: typeof candidate.row3 === "string" && candidate.row3.trim() ? candidate.row3.trim() : defaults.row3,
    row4: typeof candidate.row4 === "string" && candidate.row4.trim() ? candidate.row4.trim() : defaults.row4,
  };
}

export function syncMarqueeSettingsToGermanSource(settings: MarqueeSettings): MarqueeSettings {
  const source = settings.locales.de;

  return {
    locales: {
      de: { ...source },
      en: { ...source },
      ar: { ...source },
    },
  };
}

export function parseMarqueeSettingsValue(rawValue?: string | null): MarqueeSettings {
  const defaults = buildDefaultMarqueeSettings();

  if (!rawValue) {
    return syncMarqueeSettingsToGermanSource(defaults);
  }

  try {
    const parsed = JSON.parse(rawValue) as { locales?: Record<string, unknown> };
    const locales = parsed && typeof parsed === "object" ? parsed.locales : undefined;

    return syncMarqueeSettingsToGermanSource({
      locales: {
        ar: normalizeLocaleSettings(locales?.ar, defaults.locales.ar),
        en: normalizeLocaleSettings(locales?.en, defaults.locales.en),
        de: normalizeLocaleSettings(locales?.de, defaults.locales.de),
      },
    });
  } catch {
    return syncMarqueeSettingsToGermanSource(defaults);
  }
}

export function splitMarqueeRowItems(rawValue: string): string[] {
  return rawValue
    .split(/\r?\n|,/)
    .map((item) => item.trim())
    .filter(Boolean);
}
