import type { QuotationDocumentData } from '../types';

const LEGACY_STATIC_LABEL_DEFAULTS = {
  tel_label: 'Tel',
  email_label: 'Email',
  att_label: 'Att',
  project_label: 'Project',
  site_label: 'Location',
  quotation_date: '-',
} as const;

function normalizeStringValue(
  value: string | null | undefined,
  fallback: string,
): string {
  if (typeof value !== 'string') {
    return fallback;
  }

  const trimmed = value.trim();
  return trimmed.length > 0 ? trimmed : fallback;
}

export function resolveQuotationStaticTemplateInputs(
  documentData: QuotationDocumentData,
): Record<string, unknown> {
  const labels = documentData.pdfme.labels;
  const quotationDate = normalizeStringValue(
    documentData.pdfme.quotation_date,
    LEGACY_STATIC_LABEL_DEFAULTS.quotation_date,
  );

  return {
    quotation_date: quotationDate,
    quotation_date_data: quotationDate,
    tel_label: normalizeStringValue(
      labels.tel,
      LEGACY_STATIC_LABEL_DEFAULTS.tel_label,
    ),
    email_label: normalizeStringValue(
      labels.email,
      LEGACY_STATIC_LABEL_DEFAULTS.email_label,
    ),
    att_label: normalizeStringValue(
      labels.att,
      LEGACY_STATIC_LABEL_DEFAULTS.att_label,
    ),
    project_label: normalizeStringValue(
      labels.project,
      LEGACY_STATIC_LABEL_DEFAULTS.project_label,
    ),
    site_label: normalizeStringValue(
      labels.location,
      LEGACY_STATIC_LABEL_DEFAULTS.site_label,
    ),
  };
}
