import { importShared } from './__federation_fn_import-UR7U68uv.js';
import { j as jsxRuntimeExports } from './jsx-runtime-XI9uIe3W.js';
import { s, Q as QueueConfigTabs } from './styles-D4kR8aSb.js';
import { c as createQueuePluginDefinitionSchema } from './plugin-definition-BRLRXmzq.js';

const {getManagedPluginsForPlugin,PluginUtils} = await importShared('@baseplate-dev/project-builder-lib');

const {useBlockUnsavedChangesNavigate,useDefinitionSchema,useProjectDefinition,useResettableForm} = await importShared('@baseplate-dev/project-builder-lib/web');

const {Alert,AlertDescription,AlertTitle,FormActionBar,SectionList,SectionListSection,SectionListSectionContent,SectionListSectionDescription,SectionListSectionHeader,SectionListSectionTitle,SelectFieldController} = await importShared('@baseplate-dev/ui-components');
const {Link,useNavigate} = await importShared('@tanstack/react-router');

const {useMemo} = await importShared('react');
function QueueDefinitionEditor({
  definition: pluginMetadata,
  metadata,
  onSave
}) {
  const {
    definitionContainer,
    schemaParserContext,
    saveDefinitionWithFeedback
  } = useProjectDefinition();
  const queuePluginDefinitionSchema = useDefinitionSchema(
    createQueuePluginDefinitionSchema
  );
  const availableImplementations = getManagedPluginsForPlugin(
    schemaParserContext.pluginStore,
    metadata.key
  );
  const pgBossImplementation = availableImplementations.find(
    (i) => i.fullyQualifiedName === "@baseplate-dev/plugin-queue:pg-boss"
  );
  const defaultValues = useMemo(() => {
    if (pluginMetadata?.config) {
      return pluginMetadata.config;
    }
    const implementationKey = availableImplementations.length > 0 ? (pgBossImplementation ?? availableImplementations[0]).key : "";
    return {
      implementationPluginKey: implementationKey
    };
  }, [pluginMetadata?.config, pgBossImplementation, availableImplementations]);
  const form = useResettableForm({
    resolver: s(queuePluginDefinitionSchema),
    defaultValues
  });
  const { control, reset, handleSubmit, watch } = form;
  const implementationPluginKey = watch("implementationPluginKey");
  const selectedPlugin = implementationPluginKey && availableImplementations.find((i) => i.key === implementationPluginKey);
  const isSelectedImplementationActive = !!PluginUtils.byKey(
    definitionContainer.definition,
    implementationPluginKey
  );
  const hasSelectedImplementationChanged = implementationPluginKey !== pluginMetadata?.config?.implementationPluginKey;
  const selectedImplementationUrl = selectedPlugin ? `/plugins/edit/${selectedPlugin.key}` : void 0;
  const navigate = useNavigate();
  const onSubmit = handleSubmit(
    (data) => saveDefinitionWithFeedback(
      (draftConfig) => {
        PluginUtils.setPluginConfig(
          draftConfig,
          metadata,
          data,
          definitionContainer.pluginStore
        );
        const otherPlugins = availableImplementations.filter(
          (i) => i.key !== implementationPluginKey
        );
        for (const plugin of otherPlugins) {
          PluginUtils.disablePlugin(
            draftConfig,
            plugin.key,
            definitionContainer.parserContext
          );
        }
      },
      {
        successMessage: `Successfully saved queue plugin! ${selectedImplementationUrl && !isSelectedImplementationActive ? `Please configure the provider plugin before syncing.` : ""}`,
        onSuccess: () => {
          onSave();
          if (selectedImplementationUrl && !isSelectedImplementationActive) {
            setTimeout(() => {
              navigate({ to: selectedImplementationUrl });
            }, 100);
          }
        }
      }
    )
  );
  useBlockUnsavedChangesNavigate({ control, reset, onSubmit });
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "queue:relative queue:flex queue:h-full queue:flex-1 queue:flex-col queue:gap-4 queue:overflow-hidden", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(QueueConfigTabs, {}),
    /* @__PURE__ */ jsxRuntimeExports.jsx(
      "div",
      {
        className: "queue:mb-[--action-bar-height] queue:flex queue:flex-1 queue:overflow-y-auto",
        style: {
          "--action-bar-height": "52px"
        },
        children: /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { onSubmit, className: "queue:max-w-6xl queue:flex-1", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "queue:pb-16", children: [
            !isSelectedImplementationActive && selectedPlugin && !hasSelectedImplementationChanged && /* @__PURE__ */ jsxRuntimeExports.jsxs(Alert, { variant: "warning", children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx(AlertTitle, { children: "Implementation Provider Not Active" }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(AlertDescription, { children: /* @__PURE__ */ jsxRuntimeExports.jsxs("p", { children: [
                "The selected implementation provider is not active. Please make sure",
                " ",
                /* @__PURE__ */ jsxRuntimeExports.jsx(
                  Link,
                  {
                    to: selectedImplementationUrl,
                    className: "queue:inline-block queue:font-medium",
                    children: "configure the provider"
                  }
                ),
                " ",
                "before syncing."
              ] }) })
            ] }),
            /* @__PURE__ */ jsxRuntimeExports.jsx(SectionList, { children: /* @__PURE__ */ jsxRuntimeExports.jsxs(SectionListSection, { children: [
              /* @__PURE__ */ jsxRuntimeExports.jsxs(SectionListSectionHeader, { children: [
                /* @__PURE__ */ jsxRuntimeExports.jsx(SectionListSectionTitle, { children: "General Settings" }),
                /* @__PURE__ */ jsxRuntimeExports.jsx(SectionListSectionDescription, { children: "Configure the general settings for the queue plugin." })
              ] }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(SectionListSectionContent, { className: "queue:space-y-6", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                SelectFieldController,
                {
                  label: "Queue Provider",
                  name: "implementationPluginKey",
                  control,
                  options: availableImplementations,
                  renderItemLabel: (item) => /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "queue:flex queue:flex-col queue:gap-2", children: [
                    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "queue:text-sm queue:font-medium", children: item.displayName }),
                    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "queue:text-sm queue:text-muted-foreground", children: item.description })
                  ] }),
                  getOptionLabel: (item) => item.displayName,
                  getOptionValue: (item) => item.key,
                  description: "The queue provider to use for your project"
                }
              ) })
            ] }) })
          ] }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(FormActionBar, { form, allowSaveWithoutDirty: !pluginMetadata })
        ] })
      }
    )
  ] });
}

const {createPlatformPluginExport,webConfigSpec} = await importShared('@baseplate-dev/project-builder-lib');
const web = createPlatformPluginExport({
  dependencies: {
    webConfig: webConfigSpec
  },
  exports: {},
  initialize: ({ webConfig }, { pluginKey }) => {
    webConfig.registerWebConfigComponent(pluginKey, QueueDefinitionEditor);
    return {};
  }
});

export { web as default };
//# sourceMappingURL=__federation_expose_queueCoreWeb-B0sa7I4a.js.map
