import { defineArrayMember, defineField, defineType } from "sanity";

/**
 * Shared Portable Text block content used by both `post.body` and
 * `study.body`. Keeps the editorial mark and style set consistent across
 * News and Studies, and isolates the rich-text shape in one place.
 */
export const portableTextBlock = defineType({
  name: "portableTextBlock",
  title: "Body",
  type: "array",
  of: [
    defineArrayMember({
      type: "block",
      styles: [
        { title: "Normal", value: "normal" },
        { title: "H2", value: "h2" },
        { title: "H3", value: "h3" },
        { title: "H4", value: "h4" },
        { title: "Quote", value: "blockquote" },
      ],
      lists: [
        { title: "Bullet", value: "bullet" },
        { title: "Numbered", value: "number" },
      ],
      marks: {
        decorators: [
          { title: "Strong", value: "strong" },
          { title: "Emphasis", value: "em" },
          { title: "Code", value: "code" },
        ],
        annotations: [
          {
            name: "link",
            type: "object",
            title: "External link",
            fields: [
              defineField({
                name: "href",
                title: "URL",
                type: "url",
                validation: (Rule) =>
                  Rule.required().uri({
                    scheme: ["http", "https", "mailto", "tel"],
                  }),
              }),
              defineField({
                name: "blank",
                title: "Open in new tab",
                type: "boolean",
                initialValue: true,
              }),
            ],
          },
        ],
      },
    }),
    defineArrayMember({
      type: "image",
      options: { hotspot: true },
      fields: [
        defineField({
          name: "alt",
          title: "Alt text",
          type: "string",
          description:
            "Describe the image for screen readers. Leave blank only for purely decorative imagery.",
        }),
        defineField({
          name: "caption",
          title: "Caption",
          type: "string",
        }),
      ],
    }),
    defineArrayMember({
      type: "object",
      name: "codeBlock",
      title: "Code block",
      fields: [
        defineField({
          name: "language",
          title: "Language",
          type: "string",
          options: {
            list: [
              { title: "Plain text", value: "text" },
              { title: "TypeScript", value: "typescript" },
              { title: "JavaScript", value: "javascript" },
              { title: "JSON", value: "json" },
              { title: "Bash", value: "bash" },
              { title: "SQL", value: "sql" },
            ],
            layout: "dropdown",
          },
          initialValue: "text",
        }),
        defineField({
          name: "code",
          title: "Code",
          type: "text",
          rows: 10,
          validation: (Rule) => Rule.required(),
        }),
      ],
    }),
  ],
});
