import { importShared } from './__federation_fn_import-DO0f8H2g.js';
import { j as jsxRuntimeExports } from './jsx-runtime-XI9uIe3W.js';
import { a } from './styles-BM99U_aC.js';
import { E as EmailConfigTabs } from './email-config-tabs-Dm8XmlI8.js';
import { c as createEmailPluginDefinitionSchema } from './plugin-definition-BIIMD05o.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 EmailDefinitionEditor({
  definition: pluginMetadata,
  metadata,
  onSave
}) {
  const {
    definitionContainer,
    schemaParserContext,
    saveDefinitionWithFeedback
  } = useProjectDefinition();
  const emailPluginDefinitionSchema = useDefinitionSchema(
    createEmailPluginDefinitionSchema
  );
  const availableImplementations = getManagedPluginsForPlugin(
    schemaParserContext.pluginStore,
    metadata.key
  );
  const postmarkImplementation = availableImplementations.find(
    (i) => i.fullyQualifiedName === "@baseplate-dev/plugin-email:postmark"
  );
  const defaultValues = useMemo(() => {
    if (pluginMetadata?.config) {
      return pluginMetadata.config;
    }
    const implementationKey = availableImplementations.length > 0 ? (postmarkImplementation ?? availableImplementations[0]).key : "";
    return {
      implementationPluginKey: implementationKey
    };
  }, [
    pluginMetadata?.config,
    postmarkImplementation,
    availableImplementations
  ]);
  const form = useResettableForm({
    resolver: a(emailPluginDefinitionSchema),
    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
        );
        const otherPlugins = availableImplementations.filter(
          (i) => i.key !== implementationPluginKey
        );
        for (const plugin of otherPlugins) {
          PluginUtils.disablePlugin(
            draftConfig,
            plugin.key,
            definitionContainer.parserContext
          );
        }
      },
      {
        successMessage: `Successfully saved email 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: "email:relative email:flex email:h-full email:flex-1 email:flex-col email:gap-4 email:overflow-hidden", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(EmailConfigTabs, {}),
    /* @__PURE__ */ jsxRuntimeExports.jsx(
      "div",
      {
        className: "email:mb-[--action-bar-height] email:flex email:flex-1 email:overflow-y-auto",
        style: {
          "--action-bar-height": "52px"
        },
        children: /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { onSubmit, className: "email:max-w-6xl email:flex-1", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "email: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: "email:inline-block email: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 email plugin." })
              ] }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(SectionListSectionContent, { className: "email:space-y-6", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                SelectFieldController,
                {
                  label: "Email Provider",
                  name: "implementationPluginKey",
                  control,
                  options: availableImplementations,
                  renderItemLabel: (item) => /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "email:flex email:flex-col email:gap-2", children: [
                    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "email:text-sm email:font-medium", children: item.displayName }),
                    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "email:text-sm email:text-muted-foreground", children: item.description })
                  ] }),
                  getOptionLabel: (item) => item.displayName,
                  getOptionValue: (item) => item.key,
                  description: "The email provider to use for your project"
                }
              ) })
            ] }) })
          ] }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(FormActionBar, { form, allowSaveWithoutDirty: !pluginMetadata })
        ] })
      }
    )
  ] });
}

const {createPluginModule,webConfigSpec} = await importShared('@baseplate-dev/project-builder-lib');
const web = createPluginModule({
  name: "web",
  dependencies: {
    webConfig: webConfigSpec
  },
  initialize: ({ webConfig }, { pluginKey }) => {
    webConfig.components.set(pluginKey, EmailDefinitionEditor);
  }
});

export { web as default };
//# sourceMappingURL=__federation_expose_emailCoreWeb-C8OYNT4d.js.map
