export const DOCUMENT_SEQUENCE_DEFAULT_FORMAT = '{prefix}{period}-{running}';
export const DOCUMENT_SEQUENCE_DEFAULT_RESET_POLICY = 'period';
export const DOCUMENT_SEQUENCE_GENERIC_PRODUCT_TYPE = 'all';
export const DOCUMENT_SEQUENCE_LEGACY_PRODUCT_TYPE = 'legacy';

export const DOCUMENT_SEQUENCE_PRODUCT_TYPE_CODES: Record<string, string> = {
  crane: 'CR',
  dockdoor: 'DK',
  dock_door: 'DK',
  solarcell: 'SC',
  solar_cell: 'SC',
  solar: 'SC',
  service: 'SV',
  sparepart: 'SP',
  spare_part: 'SP'
};

export const DOCUMENT_SEQUENCE_ORGANIZATION_CODES: Record<string, string> = {
  alla: 'A',
  'alla-demo': 'A',
  onvalla: 'O'
};

export function normalizeDocumentSequenceProductType(value?: string | null) {
  return value?.trim().toLowerCase().replace(/[\s-]+/g, '_') || DOCUMENT_SEQUENCE_GENERIC_PRODUCT_TYPE;
}

export function formatDocumentSequencePeriod(date: Date) {
  const year = String(date.getFullYear()).slice(-2);
  const month = String(date.getMonth() + 1).padStart(2, '0');
  return `${year}${month}`;
}

export function resolveDocumentSequenceProductTypeCode(productType: string) {
  return DOCUMENT_SEQUENCE_PRODUCT_TYPE_CODES[normalizeDocumentSequenceProductType(productType)] ?? null;
}

export function resolveDocumentSequenceOrganizationCode(slug?: string | null) {
  return slug ? DOCUMENT_SEQUENCE_ORGANIZATION_CODES[slug.trim().toLowerCase()] ?? null : null;
}
