"use client";
import { jsx } from "react/jsx-runtime";
import { notFound, useRouter } from "next/navigation";
import { useParams } from "next/navigation";
import { useEffect, useState } from "react";
import { EntityFormWrapper } from "../../entities/wrappers/EntityFormWrapper.js";
import { BuilderEditorView } from "../../dashboard/block-editor/builder-editor-view.js";
import { Alert, AlertDescription } from "../../ui/alert.js";
import { useEntityConfig } from "../../../hooks/useEntityConfig.js";
import { getEntityData } from "../../../lib/api/entities.js";
function PatternEditPage() {
  var _a;
  const params = useParams();
  const router = useRouter();
  const [initialData, setInitialData] = useState(null);
  const [dataLoading, setDataLoading] = useState(true);
  const entitySlug = "patterns";
  const entityId = params.id;
  const { config: entityConfig, isLoading: configLoading } = useEntityConfig(entitySlug);
  useEffect(() => {
    async function loadEntityData() {
      var _a2;
      if (!entityId || !entityConfig) {
        setDataLoading(false);
        return;
      }
      try {
        if (!((_a2 = entityConfig.builder) == null ? void 0 : _a2.enabled)) {
          const data = await getEntityData(entitySlug, entityId, true);
          setInitialData(data);
        }
      } catch (error) {
        console.error("Error loading pattern:", error);
        setInitialData(null);
      } finally {
        setDataLoading(false);
      }
    }
    if (entityConfig) {
      loadEntityData();
    }
  }, [entityId, entityConfig]);
  if (configLoading || dataLoading) {
    return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-[400px]", children: /* @__PURE__ */ jsx("div", { className: "animate-spin rounded-full h-8 w-8 border-b-2 border-primary" }) });
  }
  if (!entityConfig || !entityConfig.enabled) {
    return /* @__PURE__ */ jsx(Alert, { children: /* @__PURE__ */ jsx(AlertDescription, { children: "Patterns entity is not configured or not enabled." }) });
  }
  if ((_a = entityConfig.builder) == null ? void 0 : _a.enabled) {
    return /* @__PURE__ */ jsx(
      BuilderEditorView,
      {
        entitySlug,
        entityConfig,
        id: entityId,
        mode: "edit"
      }
    );
  }
  if (!initialData) {
    notFound();
  }
  return /* @__PURE__ */ jsx(
    EntityFormWrapper,
    {
      entityType: entitySlug,
      id: entityId,
      mode: "edit",
      onSuccess: () => {
        router.push(`/dashboard/${entitySlug}/${entityId}`);
      },
      onError: (error) => {
        console.error("Error updating pattern:", error);
      }
    }
  );
}
export {
  PatternEditPage as default
};
