import { EXPORT_DATE_LOCALE } from "../constants/export-layout";
import type { ResumeTemplate } from "../constants/resume";
import { isResumeTemplate, RESUME_TEMPLATE_DEFAULT } from "../constants/resume";

/** Untrusted cover-letter content arriving from persistence or API boundaries. */
type CoverLetterContentInput =
  | string
  | Readonly<Record<string, string | ReadonlyArray<string> | undefined>>;

const EXPORT_DATE_FORMATTER = new Intl.DateTimeFormat(EXPORT_DATE_LOCALE, {
  year: "numeric",
  month: "long",
  day: "numeric",
});

const normalizeParagraphInput = (value: string | ReadonlyArray<string> | undefined): string[] => {
  if (typeof value === "string") {
    const trimmed = value.trim();
    return trimmed.length > 0 ? [trimmed] : [];
  }

  if (Array.isArray(value)) {
    return value
      .filter((entry): entry is string => typeof entry === "string")
      .map((entry) => entry.trim())
      .filter((entry) => entry.length > 0);
  }

  return [];
};

/**
 * Collects only defined, non-empty string values.
 */
export const collectDefinedStringValues = (values: ReadonlyArray<string | undefined>): string[] =>
  values.filter((value): value is string => typeof value === "string" && value.trim().length > 0);

/**
 * Resolves the canonical resume export template from explicit and persisted values.
 */
export const resolveResumeExportTemplate = (
  templateName?: string,
  resumeTemplate?: string,
): ResumeTemplate => {
  if (isResumeTemplate(templateName)) {
    return templateName;
  }

  if (isResumeTemplate(resumeTemplate)) {
    return resumeTemplate;
  }

  return RESUME_TEMPLATE_DEFAULT;
};

/**
 * CSS theme class applied to the resume preview shell for a template.
 * Class definitions are generated from RESUME_EXPORT_THEME_CONFIGS into
 * assets/css/tokens-resume-preview.generated.css (see scripts/generate-resume-preview-css.ts).
 */
export const resumePreviewThemeClass = (template: ResumeTemplate): string =>
  `resume-preview-theme-${template}`;

/**
 * Formats export dates with the shared locale contract.
 */
export const formatExportDate = (date: Date): string => EXPORT_DATE_FORMATTER.format(date);

/**
 * Normalizes cover-letter content into canonical paragraph blocks for all export renderers.
 */
export const toCoverLetterParagraphs = (content: CoverLetterContentInput): string[] => {
  if (typeof content === "string") {
    return content
      .split("\n\n")
      .map((entry) => entry.trim())
      .filter((entry) => entry.length > 0);
  }

  return [
    ...normalizeParagraphInput(content.opening).concat(
      normalizeParagraphInput(content.introduction),
    ),
    ...normalizeParagraphInput(content.body).concat(normalizeParagraphInput(content.main)),
    ...normalizeParagraphInput(content.closing).concat(normalizeParagraphInput(content.conclusion)),
  ];
};
