import type {
	StrapiComponentUnion,
	StrapiContactComponent,
	StrapiFooterResponse,
	StrapiHomepageResponse,
	StrapiProfileComponent,
	StrapiProjectComponent,
	StrapiSkillComponent
} from '$lib/types/strapi';
import type {
	Contact,
	Footer,
	Header,
	HeaderLink,
	Profile,
	Projects,
	Skills
} from '$lib/types/data';
import type { ComponentKeys } from '$lib/types/strapi';
import type { Component } from 'svelte';
import { env as publicEnv } from '$env/dynamic/public';

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export const cmsComponentMapping: Record<ComponentKeys, Promise<{ default: Component<any> }>> = {
	'shared.profile': import('$lib/components/Profile.svelte'),
	'shared.project': import('$lib/components/Projects.svelte'),
	'shared.skill': import('$lib/components/Skills.svelte'),
	'shared.contact': import('$lib/components/Contact.svelte')
};

export const mapHeaderData = (data: StrapiHomepageResponse): Header => {
	const links: HeaderLink[] = data.data.components.map((component) => {
		if (component.__component === 'shared.profile') {
			return {
				label: 'Profile',
				href: `#${component.profile.anchor_id}`
			};
		} else if (component.__component === 'shared.project') {
			return {
				label: component.project.title,
				href: `#${component.project.anchor_id}`
			};
		} else if (component.__component === 'shared.skill') {
			return {
				label: component.skill.title,
				href: `#${component.skill.anchor_id}`
			};
		}

		return {
			label: component.contact.title,
			href: `#${component.contact.anchor_id}`
		};
	});

	return { links };
};

export const mapFooterData = (data: StrapiFooterResponse): Footer => {
	return {
		copyright: data.data.copyright,
		links: {
			git: data.data.git,
			linkedin: data.data.linkedin
		}
	};
};

const mapProfileData = (data: StrapiProfileComponent): Profile => {
	return {
		name: data.profile.name,
		title: data.profile.title,
		text: data.profile.description,
		imageUrl: publicEnv.PUBLIC_STRAPI_URL + data.profile.image.formats.small.url,
		anchorId: data.profile.anchor_id
	};
};

const mapProjectData = (data: StrapiProjectComponent): Projects => {
	return {
		title: data.project.title,
		subtitle: data.project.subtitle,
		anchorId: data.project.anchor_id,
		entries: data.project.entries.map((entry) => ({
			title: entry.front_title,
			description: entry.front_description,
			taskHeadline: entry.back_title,
			tasks: entry.back_description,
			toolsHeadline: entry.back_title_second,
			tools: entry.back_content_list
		}))
	};
};

const mapSkillData = (data: StrapiSkillComponent): Skills => {
	return {
		title: data.skill.title,
		subtitle: data.skill.subtitle,
		anchorId: data.skill.anchor_id,
		entries: data.skill.entries.map((entry) => ({
			title: entry.title,
			icon: entry.icon,
			items: entry.content
		}))
	};
};

const mapContactData = (data: StrapiContactComponent): Contact => {
	return {
		anchorId: data.contact.anchor_id,
		subtitle: data.contact.subtitle,
		title: data.contact.title,
		form: {
			title: data.contact.form_title,
			subjectPlaceholder: data.contact.subject_placeholder,
			messagePlaceholder: data.contact.message_placeholder,
			submit: data.contact.cta_text
		}
	};
};

export const mapComponentData = (data: StrapiComponentUnion) => {
	if (data.__component === 'shared.profile') {
		return mapProfileData(data);
	} else if (data.__component === 'shared.project') {
		return mapProjectData(data);
	} else if (data.__component === 'shared.skill') {
		return mapSkillData(data);
	}

	return mapContactData(data);
};
