'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const client = require('@btst/stack/plugins/client');
const yar = require('@btst/yar');
const plugins_formBuilder_queryKeys = require('../../../../../../plugins/form-builder/query-keys.cjs');

const FormListPageComponent = React.lazy(
  () => import('./components/pages/form-list-page.cjs').then((m) => ({
    default: m.FormListPageComponent
  }))
);
const FormBuilderPageComponent = React.lazy(
  () => import('./components/pages/form-builder-page.cjs').then((m) => ({
    default: m.FormBuilderPageComponent
  }))
);
const SubmissionsPageComponent = React.lazy(
  () => import('./components/pages/submissions-page.cjs').then((m) => ({
    default: m.SubmissionsPageComponent
  }))
);
function createFormListLoader(config) {
  return async () => {
    if (typeof window === "undefined") {
      const { queryClient, apiBasePath, apiBaseURL, headers, hooks } = config;
      const context = {
        path: "/forms",
        isSSR: true,
        apiBaseURL,
        apiBasePath,
        headers
      };
      try {
        if (hooks?.beforeLoadFormList) {
          await client.runClientHookWithShim(
            () => hooks.beforeLoadFormList(context),
            "Load prevented by beforeLoadFormList hook"
          );
        }
        const client$1 = client.createApiClient({
          baseURL: apiBaseURL,
          basePath: apiBasePath
        });
        const queries = plugins_formBuilder_queryKeys.createFormBuilderQueryKeys(client$1, headers);
        const limit = 20;
        const listQuery = queries.forms.list({ limit, offset: 0 });
        await queryClient.prefetchInfiniteQuery({
          queryKey: listQuery.queryKey,
          queryFn: async ({ pageParam = 0 }) => {
            const response = await client$1("/forms", {
              method: "GET",
              query: { limit, offset: pageParam },
              headers
            });
            if (typeof response === "object" && response !== null && "error" in response && response.error) {
              throw new Error(String(response.error));
            }
            return response.data;
          },
          initialPageParam: 0
        });
        if (hooks?.afterLoadFormList) {
          await hooks.afterLoadFormList(context);
        }
        const queryState = queryClient.getQueryState(listQuery.queryKey);
        if (queryState?.error && hooks?.onLoadError) {
          const error = queryState.error instanceof Error ? queryState.error : new Error(String(queryState.error));
          await hooks.onLoadError(error, context);
        }
      } catch (error) {
        if (client.isConnectionError(error)) {
          console.warn(
            "[btst/form-builder] route.loader() failed \u2014 no server running at build time. Use myStack.api.formBuilder.prefetchForRoute() for SSG data prefetching."
          );
        }
        if (hooks?.onLoadError) {
          await hooks.onLoadError(error, context);
        }
      }
    }
  };
}
function createFormBuilderLoader(id, config) {
  return async () => {
    if (typeof window === "undefined") {
      const { queryClient, apiBasePath, apiBaseURL, headers, hooks } = config;
      const context = {
        path: id ? `/forms/${id}/edit` : "/forms/new",
        params: id ? { id } : {},
        isSSR: true,
        apiBaseURL,
        apiBasePath,
        headers
      };
      try {
        if (hooks?.beforeLoadFormBuilder) {
          await client.runClientHookWithShim(
            () => hooks.beforeLoadFormBuilder(id, context),
            "Load prevented by beforeLoadFormBuilder hook"
          );
        }
        const client$1 = client.createApiClient({
          baseURL: apiBaseURL,
          basePath: apiBasePath
        });
        const queries = plugins_formBuilder_queryKeys.createFormBuilderQueryKeys(client$1, headers);
        if (id) {
          await queryClient.prefetchQuery(queries.forms.byId(id));
        }
        if (hooks?.afterLoadFormBuilder) {
          await hooks.afterLoadFormBuilder(id, context);
        }
        if (id) {
          const queryState = queryClient.getQueryState(
            queries.forms.byId(id).queryKey
          );
          if (queryState?.error && hooks?.onLoadError) {
            const error = queryState.error instanceof Error ? queryState.error : new Error(String(queryState.error));
            await hooks.onLoadError(error, context);
          }
        }
      } catch (error) {
        if (client.isConnectionError(error)) {
          console.warn(
            "[btst/form-builder] route.loader() failed \u2014 no server running at build time. Use myStack.api.formBuilder.prefetchForRoute() for SSG data prefetching."
          );
        }
        if (hooks?.onLoadError) {
          await hooks.onLoadError(error, context);
        }
      }
    }
  };
}
function createSubmissionsLoader(formId, config) {
  return async () => {
    if (typeof window === "undefined") {
      const { queryClient, apiBasePath, apiBaseURL, headers, hooks } = config;
      const context = {
        path: `/forms/${formId}/submissions`,
        params: { formId },
        isSSR: true,
        apiBaseURL,
        apiBasePath,
        headers
      };
      try {
        if (hooks?.beforeLoadSubmissions) {
          await client.runClientHookWithShim(
            () => hooks.beforeLoadSubmissions(formId, context),
            "Load prevented by beforeLoadSubmissions hook"
          );
        }
        const client$1 = client.createApiClient({
          baseURL: apiBaseURL,
          basePath: apiBasePath
        });
        const queries = plugins_formBuilder_queryKeys.createFormBuilderQueryKeys(client$1, headers);
        const limit = 20;
        await queryClient.prefetchQuery(queries.forms.byId(formId));
        const submissionsQuery = queries.formSubmissions.list({
          formId,
          limit,
          offset: 0
        });
        await queryClient.prefetchInfiniteQuery({
          queryKey: submissionsQuery.queryKey,
          queryFn: async ({ pageParam = 0 }) => {
            const response = await client$1(
              "/forms/:formId/submissions",
              {
                method: "GET",
                params: { formId },
                query: { limit, offset: pageParam },
                headers
              }
            );
            if (typeof response === "object" && response !== null && "error" in response && response.error) {
              throw new Error(String(response.error));
            }
            return response.data;
          },
          initialPageParam: 0
        });
        if (hooks?.afterLoadSubmissions) {
          await hooks.afterLoadSubmissions(formId, context);
        }
        const formState = queryClient.getQueryState(
          queries.forms.byId(formId).queryKey
        );
        const submissionsState = queryClient.getQueryState(
          submissionsQuery.queryKey
        );
        const queryError = formState?.error || submissionsState?.error;
        if (queryError && hooks?.onLoadError) {
          const error = queryError instanceof Error ? queryError : new Error(String(queryError));
          await hooks.onLoadError(error, context);
        }
      } catch (error) {
        if (client.isConnectionError(error)) {
          console.warn(
            "[btst/form-builder] route.loader() failed \u2014 no server running at build time. Use myStack.api.formBuilder.prefetchForRoute() for SSG data prefetching."
          );
        }
        if (hooks?.onLoadError) {
          await hooks.onLoadError(error, context);
        }
      }
    }
  };
}
function createFormListMeta() {
  return () => {
    const title = "Forms";
    return [
      { title },
      { name: "title", content: title },
      { name: "robots", content: "noindex" }
    ];
  };
}
function createFormBuilderMeta(id, config) {
  return () => {
    const { queryClient, apiBasePath, apiBaseURL } = config;
    let formName = "";
    if (id) {
      const client$1 = client.createApiClient({
        baseURL: apiBaseURL,
        basePath: apiBasePath
      });
      const queries = plugins_formBuilder_queryKeys.createFormBuilderQueryKeys(client$1);
      const form = queryClient.getQueryData(queries.forms.byId(id).queryKey);
      formName = form?.name || "";
    }
    const title = id ? `Edit ${formName || "Form"}` : "New Form";
    return [
      { title },
      { name: "title", content: title },
      { name: "robots", content: "noindex" }
    ];
  };
}
function createSubmissionsMeta(formId, config) {
  return () => {
    const { queryClient, apiBasePath, apiBaseURL } = config;
    const client$1 = client.createApiClient({
      baseURL: apiBaseURL,
      basePath: apiBasePath
    });
    const queries = plugins_formBuilder_queryKeys.createFormBuilderQueryKeys(client$1);
    const form = queryClient.getQueryData(
      queries.forms.byId(formId).queryKey
    );
    const title = form?.name ? `${form.name} Submissions` : "Submissions";
    return [
      { title },
      { name: "title", content: title },
      { name: "robots", content: "noindex" }
    ];
  };
}
const formBuilderClientPlugin = (config) => client.defineClientPlugin({
  name: "form-builder",
  routes: () => ({
    formList: yar.createRoute("/forms", () => {
      const CustomFormList = config.pageComponents?.formList;
      return {
        PageComponent: CustomFormList ?? (() => /* @__PURE__ */ jsxRuntime.jsx(FormListPageComponent, {})),
        loader: createFormListLoader(config),
        meta: createFormListMeta()
      };
    }),
    newForm: yar.createRoute("/forms/new", () => {
      const CustomNewForm = config.pageComponents?.newForm;
      return {
        PageComponent: CustomNewForm ?? (() => /* @__PURE__ */ jsxRuntime.jsx(FormBuilderPageComponent, {})),
        loader: createFormBuilderLoader(void 0, config),
        meta: createFormBuilderMeta(void 0, config)
      };
    }),
    editForm: yar.createRoute("/forms/:id/edit", ({ params }) => {
      const CustomEditForm = config.pageComponents?.editForm;
      return {
        PageComponent: CustomEditForm ? () => /* @__PURE__ */ jsxRuntime.jsx(CustomEditForm, { id: params.id }) : () => /* @__PURE__ */ jsxRuntime.jsx(FormBuilderPageComponent, { id: params.id }),
        loader: createFormBuilderLoader(params.id, config),
        meta: createFormBuilderMeta(params.id, config)
      };
    }),
    submissions: yar.createRoute("/forms/:id/submissions", ({ params }) => {
      const CustomSubmissions = config.pageComponents?.submissions;
      return {
        PageComponent: CustomSubmissions ? () => /* @__PURE__ */ jsxRuntime.jsx(CustomSubmissions, { formId: params.id }) : () => /* @__PURE__ */ jsxRuntime.jsx(SubmissionsPageComponent, { formId: params.id }),
        loader: createSubmissionsLoader(params.id, config),
        meta: createSubmissionsMeta(params.id, config)
      };
    })
  }),
  sitemap: async () => {
    return [];
  }
});

exports.formBuilderClientPlugin = formBuilderClientPlugin;
