"use client";
import { jsxs, jsx } from 'react/jsx-runtime';
import { usePluginOverrides, useBasePath } from '@btst/stack/context';
import { AddPostForm } from '../forms/post-forms.mjs';
import { PageHeader } from '../shared/page-header.mjs';
import { PageWrapper } from '../shared/page-wrapper.mjs';
import { BLOG_LOCALIZATION } from '../../localization/index.mjs';
import { useRouteLifecycle } from '../../../../../../../ui/src/hooks/use-route-lifecycle.mjs';
import { useRegisterPageAIContext } from '@btst/stack/plugins/ai-chat/client/context';
import { useRef, useCallback } from 'react';
import { createFillBlogFormHandler } from './fill-blog-form-handler.mjs';

function NewPostPage() {
  const overrides = usePluginOverrides("blog", {
    localization: BLOG_LOCALIZATION
  });
  const { localization, navigate } = overrides;
  const basePath = useBasePath();
  useRouteLifecycle({
    routeName: "newPost",
    context: {
      path: "/blog/new",
      isSSR: typeof window === "undefined"
    },
    overrides,
    beforeRenderHook: (overrides2, context) => {
      if (overrides2.onBeforeNewPostPageRendered) {
        return overrides2.onBeforeNewPostPageRendered(context);
      }
      return true;
    }
  });
  const formRef = useRef(null);
  const handleFormReady = useCallback((form) => {
    formRef.current = form;
  }, []);
  useRegisterPageAIContext({
    routeName: "blog-new-post",
    pageDescription: "User is creating a new blog post in the admin editor. IMPORTANT: When asked to write, draft, or create a blog post, you MUST call the fillBlogForm tool to populate the form fields directly \u2014 do NOT just output the text in your response.",
    suggestions: [
      "Write a post about AI trends",
      "Draft an intro paragraph",
      "Suggest 5 tags for this post"
    ],
    clientTools: {
      fillBlogForm: createFillBlogFormHandler(
        formRef,
        "Form filled successfully"
      )
    }
  });
  const handleClose = () => {
    navigate(`${basePath}/blog`);
  };
  const handleSuccess = (post) => {
    if (post.published) {
      navigate(`${basePath}/blog`);
    } else {
      navigate(`${basePath}/blog/drafts`);
    }
  };
  return /* @__PURE__ */ jsxs(PageWrapper, { className: "gap-6", testId: "new-post-page", children: [
    /* @__PURE__ */ jsx(
      PageHeader,
      {
        title: localization.BLOG_POST_ADD_TITLE,
        description: localization.BLOG_POST_ADD_DESCRIPTION
      }
    ),
    /* @__PURE__ */ jsx(
      AddPostForm,
      {
        onClose: handleClose,
        onSuccess: handleSuccess,
        onFormReady: handleFormReady
      }
    )
  ] });
}

export { NewPostPage };
