import { View } from "@react-pdf/renderer";
import React from "react";
import z from "zod";
import { defineComponent } from "../define-component.js";
import { detailsItemComponent } from "../atoms/details-item-component.js";
import { sectionHeaderComponent } from "../atoms/section-header-component.js";
import { joinComponents } from "../utils.js";
import { workSectionComponent } from "./work-section-component.js";
import { detailsListComponent } from "../atoms/details-list-component.js";

export const publicationsSectionSchema = z.object({
  config: z
    .object({
      wrap: z.object({ publications: z.boolean().nullish() }).nullish(),
    })
    .nullish(),
  strings: z
    .object({
      publications: z.string().default(""),
    })
    .default({}),
  publications: z
    .array(
      z.object({
        $id: z.string().nullish(),
        name: z.string(),
        publisher: z.string().nullish(),
        releaseDate: z.string().nullish(),
        url: z.string().url().nullish(),
        summary: z.string().nullish(),
      }),
    )
    .nullish(),
});

export const publicationsSectionComponent = defineComponent({
  name: "publications" as const,
  schema: publicationsSectionSchema,
  component: ({ spec, styles, getComponent }) => {
    const SectionHeader = getComponent(sectionHeaderComponent);
    const DetailsList = getComponent(detailsListComponent);
    const DetailsItem = getComponent(detailsItemComponent);
    if (!spec.publications) return null;
    return (
      <View
        style={styles.container}
        wrap={spec.config?.wrap?.publications ?? true}
      >
        <SectionHeader style={styles.header}>
          {spec.strings?.publications}
        </SectionHeader>
        <DetailsList>
          {spec.publications.map((section, index) => (
            <View key={index} style={styles.item}>
              <DetailsItem
                style={styles.item}
                title={
                  section.url
                    ? `[${section.name}](${section.url})`
                    : section.name
                }
                right={section.releaseDate}
                details={joinComponents([section.publisher])}
                separator=", "
                summary={section.summary}
              />
            </View>
          ))}
        </DetailsList>
      </View>
    );
  },
  defaultStyles: workSectionComponent.defaultStyles,
});
