"use client";
import { z } from 'zod';
import { Button } from '../../../../../ui/src/components/button.mjs';
import { Badge } from '../../../../../ui/src/components/badge.mjs';
import { AccordionContent, AccordionTrigger, AccordionItem, Accordion } from '../../../../../ui/src/components/accordion.mjs';
import { CardFooter, CardContent, CardDescription, CardTitle, CardHeader, Card } from '../../../../../ui/src/components/card.mjs';
import { Separator } from '../../../../../ui/src/components/separator.mjs';
import { Flexbox } from '../../../../../ui/src/components/ui-builder/components/flexbox.mjs';
import { Grid } from '../../../../../ui/src/components/ui-builder/components/grid.mjs';
import { CodePanel } from '../../../../../ui/src/components/ui-builder/components/code-panel.mjs';
import { Markdown } from '../../../../../ui/src/components/ui-builder/components/markdown.mjs';
import { Icon, iconNames } from '../../../../../ui/src/components/ui-builder/components/icon.mjs';
import { classNameFieldOverrides, commonFieldOverrides, childrenFieldOverrides, iconNameFieldOverrides, childrenAsTipTapFieldOverrides, childrenAsTextareaFieldOverrides } from '../../../../../ui/src/lib/ui-builder/registry/form-field-overrides.mjs';

const primitiveComponentDefinitions = {
  a: {
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional(),
      href: z.string().optional(),
      target: z.enum(["_blank", "_self", "_parent", "_top"]).optional().default("_self"),
      rel: z.enum(["noopener", "noreferrer", "nofollow"]).optional(),
      title: z.string().optional(),
      download: z.boolean().optional().default(false)
    }),
    fieldOverrides: commonFieldOverrides()
  },
  img: {
    schema: z.object({
      className: z.string().optional(),
      src: z.string().default("https://placehold.co/200"),
      alt: z.string().optional(),
      width: z.coerce.number().optional(),
      height: z.coerce.number().optional()
    }),
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides()
    }
  },
  div: {
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    fieldOverrides: commonFieldOverrides()
  },
  iframe: {
    schema: z.object({
      className: z.string().optional(),
      src: z.string().default(
        "https://www.youtube.com/embed/dQw4w9WgXcQ?si=oc74qTYUBuCsOJwL"
      ),
      title: z.string().optional(),
      width: z.coerce.number().optional(),
      height: z.coerce.number().optional(),
      frameBorder: z.number().optional(),
      allowFullScreen: z.boolean().optional(),
      allow: z.string().optional(),
      referrerPolicy: z.enum([
        "no-referrer",
        "no-referrer-when-downgrade",
        "origin",
        "origin-when-cross-origin",
        "same-origin",
        "strict-origin",
        "strict-origin-when-cross-origin",
        "unsafe-url"
      ]).optional()
    }),
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides()
    }
  },
  span: {
    schema: z.object({
      className: z.string().optional(),
      children: z.string().optional()
    }),
    defaultChildren: "Text",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides(),
      children: (layer) => childrenAsTextareaFieldOverrides(layer)
    }
  },
  h1: {
    schema: z.object({
      className: z.string().optional(),
      children: z.string().optional()
    }),
    defaultChildren: "Heading 1",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides(),
      children: (layer) => childrenAsTextareaFieldOverrides(layer)
    }
  },
  h2: {
    schema: z.object({
      className: z.string().optional(),
      children: z.string().optional()
    }),
    defaultChildren: "Heading 2",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides(),
      children: (layer) => childrenAsTextareaFieldOverrides(layer)
    }
  },
  h3: {
    schema: z.object({
      className: z.string().optional(),
      children: z.string().optional()
    }),
    defaultChildren: "Heading 3",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides(),
      children: (layer) => childrenAsTextareaFieldOverrides(layer)
    }
  },
  p: {
    schema: z.object({
      className: z.string().optional(),
      children: z.string().optional()
    }),
    defaultChildren: "Paragraph",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides(),
      children: (layer) => childrenAsTextareaFieldOverrides(layer)
    }
  }
};
const complexComponentDefinitions = {
  Button: {
    component: Button,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional(),
      asChild: z.boolean().optional(),
      variant: z.enum([
        "default",
        "destructive",
        "outline",
        "secondary",
        "ghost",
        "link"
      ]).default("default"),
      size: z.enum(["default", "sm", "lg", "icon"]).default("default")
    }),
    from: "@/components/ui/button",
    defaultChildren: [
      {
        id: "button-text",
        type: "span",
        name: "span",
        props: {},
        children: "Button"
      }
    ],
    fieldOverrides: commonFieldOverrides()
  },
  Badge: {
    component: Badge,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional(),
      variant: z.enum(["default", "secondary", "destructive", "outline"]).default("default")
    }),
    from: "@/components/ui/badge",
    defaultChildren: [
      {
        id: "badge-text",
        type: "span",
        name: "span",
        props: {},
        children: "Badge"
      }
    ],
    fieldOverrides: commonFieldOverrides()
  },
  Flexbox: {
    component: Flexbox,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional(),
      direction: z.enum(["row", "column", "rowReverse", "columnReverse"]).default("row"),
      justify: z.enum(["start", "end", "center", "between", "around", "evenly"]).default("start"),
      align: z.enum(["start", "end", "center", "baseline", "stretch"]).default("start"),
      wrap: z.enum(["wrap", "nowrap", "wrapReverse"]).default("nowrap"),
      gap: z.preprocess(
        (val) => typeof val === "number" ? String(val) : val,
        z.enum(["0", "1", "2", "4", "8"]).default("1")
      ).transform(Number)
    }),
    from: "@/components/ui/ui-builder/flexbox",
    fieldOverrides: commonFieldOverrides()
  },
  Grid: {
    component: Grid,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional(),
      columns: z.enum(["auto", "1", "2", "3", "4", "5", "6", "7", "8"]).default("1"),
      autoRows: z.enum(["none", "min", "max", "fr"]).default("none"),
      justify: z.enum(["start", "end", "center", "between", "around", "evenly"]).default("start"),
      align: z.enum(["start", "end", "center", "baseline", "stretch"]).default("start"),
      templateRows: z.enum(["none", "1", "2", "3", "4", "5", "6"]).default("none").transform((val) => val === "none" ? val : Number(val)),
      gap: z.preprocess(
        (val) => typeof val === "number" ? String(val) : val,
        z.enum(["0", "1", "2", "4", "8"]).default("0")
      ).transform(Number)
    }),
    from: "@/components/ui/ui-builder/grid",
    fieldOverrides: commonFieldOverrides()
  },
  CodePanel: {
    component: CodePanel,
    schema: z.object({
      className: z.string().optional()
    }),
    from: "@/components/ui/ui-builder/code-panel",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides()
    }
  },
  Markdown: {
    component: Markdown,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    from: "@/components/ui/ui-builder/markdown",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides(),
      children: (layer) => childrenAsTipTapFieldOverrides(layer)
    }
  },
  Icon: {
    component: Icon,
    schema: z.object({
      className: z.string().optional(),
      iconName: z.enum([...iconNames]).default("Image"),
      size: z.enum(["small", "medium", "large"]).default("medium"),
      color: z.enum([
        "accent",
        "accentForeground",
        "primary",
        "primaryForeground",
        "secondary",
        "secondaryForeground",
        "destructive",
        "destructiveForeground",
        "muted",
        "mutedForeground",
        "background",
        "foreground"
      ]).optional(),
      rotate: z.enum(["none", "90", "180", "270"]).default("none")
    }),
    from: "@/components/ui/ui-builder/icon",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides(),
      iconName: (layer) => iconNameFieldOverrides(layer)
    }
  },
  Accordion: {
    component: Accordion,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional(),
      type: z.enum(["single", "multiple"]).default("single"),
      collapsible: z.boolean().optional()
    }),
    from: "@/components/ui/accordion",
    defaultChildren: [
      {
        id: "acc-item-1",
        type: "AccordionItem",
        name: "AccordionItem",
        props: { value: "item-1" },
        children: [
          {
            id: "acc-trigger-1",
            type: "AccordionTrigger",
            name: "AccordionTrigger",
            props: {},
            children: [
              {
                id: "acc-trigger-text",
                type: "span",
                name: "span",
                props: {},
                children: "Accordion Item #1"
              }
            ]
          },
          {
            id: "acc-content-1",
            type: "AccordionContent",
            name: "AccordionContent",
            props: {},
            children: [
              {
                id: "acc-content-text",
                type: "span",
                name: "span",
                props: {},
                children: "Accordion Content Text"
              }
            ]
          }
        ]
      }
    ],
    fieldOverrides: commonFieldOverrides()
  },
  AccordionItem: {
    component: AccordionItem,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional(),
      value: z.string().default("item-1")
    }),
    from: "@/components/ui/accordion",
    fieldOverrides: commonFieldOverrides()
  },
  AccordionTrigger: {
    component: AccordionTrigger,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    from: "@/components/ui/accordion",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides(),
      children: (layer) => childrenFieldOverrides(layer)
    }
  },
  AccordionContent: {
    component: AccordionContent,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    from: "@/components/ui/accordion",
    fieldOverrides: commonFieldOverrides()
  },
  Card: {
    component: Card,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    from: "@/components/ui/card",
    defaultChildren: [
      {
        id: "card-header",
        type: "CardHeader",
        name: "CardHeader",
        props: {},
        children: [
          {
            id: "card-title",
            type: "CardTitle",
            name: "CardTitle",
            props: {},
            children: "Card Title"
          },
          {
            id: "card-description",
            type: "CardDescription",
            name: "CardDescription",
            props: {},
            children: "Card Description"
          }
        ]
      },
      {
        id: "card-content",
        type: "CardContent",
        name: "CardContent",
        props: {},
        children: [
          {
            id: "card-content-text",
            type: "span",
            name: "span",
            props: {},
            children: "Card Content Text"
          }
        ]
      },
      {
        id: "card-footer",
        type: "CardFooter",
        name: "CardFooter",
        props: {},
        children: [
          {
            id: "card-footer-text",
            type: "span",
            name: "span",
            props: {},
            children: "Card Footer Text"
          }
        ]
      }
    ],
    fieldOverrides: commonFieldOverrides()
  },
  CardHeader: {
    component: CardHeader,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    from: "@/components/ui/card",
    fieldOverrides: commonFieldOverrides()
  },
  CardTitle: {
    component: CardTitle,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    from: "@/components/ui/card",
    fieldOverrides: commonFieldOverrides()
  },
  CardDescription: {
    component: CardDescription,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    from: "@/components/ui/card",
    fieldOverrides: commonFieldOverrides()
  },
  CardContent: {
    component: CardContent,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    from: "@/components/ui/card",
    fieldOverrides: commonFieldOverrides()
  },
  CardFooter: {
    component: CardFooter,
    schema: z.object({
      className: z.string().optional(),
      children: z.any().optional()
    }),
    from: "@/components/ui/card",
    fieldOverrides: commonFieldOverrides()
  },
  Separator: {
    component: Separator,
    schema: z.object({
      className: z.string().optional(),
      orientation: z.enum(["horizontal", "vertical"]).default("horizontal"),
      decorative: z.boolean().default(true)
    }),
    from: "@/components/ui/separator",
    fieldOverrides: {
      className: (layer) => classNameFieldOverrides()
    }
  }
};
const defaultComponentRegistry = {
  ...primitiveComponentDefinitions,
  ...complexComponentDefinitions
};
function createComponentRegistry(components) {
  return components;
}

export { complexComponentDefinitions, createComponentRegistry, defaultComponentRegistry, primitiveComponentDefinitions };
