import "./chunk-TKGWSUEI.js";
import {
  KeyboundForm
} from "./chunk-U2ZFCAPX.js";
import {
  RouteDrawer,
  useRouteModal
} from "./chunk-FWJLS7XD.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  useSalesChannel,
  useUpdateSalesChannel
} from "./chunk-DL2ZIXBO.js";
import "./chunk-5CK6XEFV.js";
import "./chunk-RHKRREUU.js";
import "./chunk-ZA2KFUFR.js";
import "./chunk-ZBWL72TB.js";
import "./chunk-NBMM2TZK.js";

// src/pages/sales-channels/sales-channel-edit/sales-channel-edit.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { useParams } from "react-router-dom";

// src/pages/sales-channels/sales-channel-edit/components/edit-sales-channel-form/edit-sales-channel-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { Button, Input, Switch, Textarea, toast } from "@medusajs/ui";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as zod from "zod";
import { jsx, jsxs } from "react/jsx-runtime";
var EditSalesChannelSchema = zod.object({
  name: zod.string().min(1),
  description: zod.string().optional(),
  is_active: zod.boolean()
});
var EditSalesChannelForm = ({
  salesChannel
}) => {
  const { t } = useTranslation();
  const { handleSuccess } = useRouteModal();
  const form = useForm({
    defaultValues: {
      name: salesChannel.name,
      description: salesChannel.description ?? "",
      is_active: !salesChannel.is_disabled
    },
    resolver: zodResolver(EditSalesChannelSchema)
  });
  const { mutateAsync, isPending } = useUpdateSalesChannel(salesChannel.id);
  const handleSubmit = form.handleSubmit(async (values) => {
    await mutateAsync(
      {
        name: values.name,
        description: values.description ?? void 0,
        is_disabled: !values.is_active
      },
      {
        onSuccess: () => {
          toast.success(t("salesChannels.toast.update"));
          handleSuccess();
        },
        onError: (error) => {
          toast.error(error.message);
        }
      }
    );
  });
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, "data-testid": "sales-channel-edit-form", children: /* @__PURE__ */ jsxs(
    KeyboundForm,
    {
      onSubmit: handleSubmit,
      className: "flex flex-1 flex-col overflow-hidden",
      children: [
        /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex max-w-full flex-1 flex-col gap-y-8 overflow-y-auto", "data-testid": "sales-channel-edit-form-body", children: [
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "name",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "sales-channel-edit-form-name-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { "data-testid": "sales-channel-edit-form-name-label", children: t("fields.name") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "sales-channel-edit-form-name-control", children: /* @__PURE__ */ jsx(Input, { ...field, size: "small", "data-testid": "sales-channel-edit-form-name-input" }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "sales-channel-edit-form-name-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "description",
              render: ({ field }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "sales-channel-edit-form-description-item", children: [
                  /* @__PURE__ */ jsx(Form.Label, { optional: true, "data-testid": "sales-channel-edit-form-description-label", children: t("fields.description") }),
                  /* @__PURE__ */ jsx(Form.Control, { "data-testid": "sales-channel-edit-form-description-control", children: /* @__PURE__ */ jsx(Textarea, { ...field, "data-testid": "sales-channel-edit-form-description-input" }) }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "sales-channel-edit-form-description-error" })
                ] });
              }
            }
          ),
          /* @__PURE__ */ jsx(
            Form.Field,
            {
              control: form.control,
              name: "is_active",
              render: ({ field: { onChange, value, ...field } }) => {
                return /* @__PURE__ */ jsxs(Form.Item, { "data-testid": "sales-channel-edit-form-enabled-item", children: [
                  /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
                    /* @__PURE__ */ jsx(Form.Label, { "data-testid": "sales-channel-edit-form-enabled-label", children: t("general.enabled") }),
                    /* @__PURE__ */ jsx(Form.Control, { "data-testid": "sales-channel-edit-form-enabled-control", children: /* @__PURE__ */ jsx(
                      Switch,
                      {
                        dir: "ltr",
                        className: "rtl:rotate-180",
                        onCheckedChange: onChange,
                        checked: value,
                        ...field,
                        "data-testid": "sales-channel-edit-form-enabled-switch"
                      }
                    ) })
                  ] }),
                  /* @__PURE__ */ jsx(Form.Hint, { "data-testid": "sales-channel-edit-form-enabled-hint", children: t("salesChannels.enabledHint") }),
                  /* @__PURE__ */ jsx(Form.ErrorMessage, { "data-testid": "sales-channel-edit-form-enabled-error" })
                ] });
              }
            }
          )
        ] }),
        /* @__PURE__ */ jsx(RouteDrawer.Footer, { "data-testid": "sales-channel-edit-form-footer", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
          /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", "data-testid": "sales-channel-edit-form-cancel-button", children: t("actions.cancel") }) }),
          /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, "data-testid": "sales-channel-edit-form-save-button", children: t("actions.save") })
        ] }) })
      ]
    }
  ) });
};

// src/pages/sales-channels/sales-channel-edit/sales-channel-edit.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var SalesChannelEdit = () => {
  const { id } = useParams();
  const { t } = useTranslation2();
  const {
    sales_channel,
    isPending: isLoading,
    isError,
    error
  } = useSalesChannel(id);
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs2(RouteDrawer, { "data-testid": "sales-channel-edit-drawer", children: [
    /* @__PURE__ */ jsx2(RouteDrawer.Header, { "data-testid": "sales-channel-edit-drawer-header", children: /* @__PURE__ */ jsx2(Heading, { className: "capitalize", "data-testid": "sales-channel-edit-drawer-heading", children: t("salesChannels.editSalesChannel") }) }),
    !isLoading && !!sales_channel && /* @__PURE__ */ jsx2(EditSalesChannelForm, { salesChannel: sales_channel })
  ] });
};
export {
  SalesChannelEdit as Component
};
