import { test } from 'vitest';
import fs from 'node:fs';
import { mapContactProps } from '@/lib/wp/viewProps/contact';
import { mapFAQProps } from '@/lib/wp/viewProps/faq';
import { mapReferencesProps } from '@/lib/wp/viewProps/references';
import { mapDiagnosticProps } from '@/lib/wp/viewProps/diagnostic';
import { mapEstimationProps } from '@/lib/wp/viewProps/estimation';

const toTypename = (layout: string) => 'Page_Sections_Sections_' + layout.split('_').map(s => s[0].toUpperCase() + s.slice(1)).join('') + 'Layout';
const camel = (o: any): any => {
  if (Array.isArray(o)) return o.map(camel);
  if (o && typeof o === 'object') return Object.fromEntries(Object.entries(o).map(([k, v]) => [k.replace(/_([a-z])/g, (_, c) => c.toUpperCase()), camel(v)]));
  return o;
};
const load = (slug: string) => {
  const j = JSON.parse(fs.readFileSync(`wordpress/seed/content/page--${slug}.json`, 'utf8'));
  return j.sections.map((s: any) => {
    const { layout, ...rest } = s;
    const sec = camel(rest);
    if (sec.logosCustom) sec.logosCustom = sec.logosCustom.map((l: any) => ({ ...l, image: { node: { sourceUrl: 'https://covalba-admin.paf-studio.dev/wp-content/uploads/' + l.image.path, altText: l.image.alt } } }));
    return { __typename: toTypename(layout), ...sec };
  });
};
const fmt = (v: any) => JSON.stringify(v, (k, val) => typeof val === 'function' ? `[icon:${(val as any).displayName ?? val.name ?? 'fn'}]` : val, 1);

test('smoke', () => {
  console.log('CONTACT', fmt(mapContactProps(load('contact'))));
  console.log('FAQ', fmt(mapFAQProps(load('faq'))));
  console.log('REFERENCES', fmt(mapReferencesProps(load('references'))));
  console.log('DIAGNOSTIC', fmt(mapDiagnosticProps(load('diagnostic'))));
  console.log('ESTIMATION', fmt(mapEstimationProps(load('estimation'))));
  console.log('EMPTY_CONTACT', fmt(mapContactProps([])));
  console.log('EMPTY_DIAG', fmt(mapDiagnosticProps([])));
});
