import {
  useComboboxData
} from "./chunk-OVPAOBZB.js";
import {
  Combobox
} from "./chunk-VBRTC2VU.js";
import {
  Form
} from "./chunk-3QSRE5LS.js";
import {
  usePromotionRuleAttributes,
  usePromotionRules
} from "./chunk-MR6REQKT.js";
import {
  useCurrentSeller
} from "./chunk-W34KDYZA.js";
import {
  sdk
} from "./chunk-DTGEFKK4.js";

// src/pages/promotions/common/edit-rules/components/rules-form-field/rules-form-field.tsx
import { XMarkMini } from "@medusajs/icons";
import { Badge, Button, Heading, IconButton, Select, Text } from "@medusajs/ui";
import { forwardRef, Fragment, useEffect as useEffect2, useRef as useRef2 } from "react";
import {
  useFieldArray,
  useWatch as useWatch2
} from "react-hook-form";
import { useTranslation as useTranslation2 } from "react-i18next";

// src/pages/promotions/common/edit-rules/components/edit-rules-form/utils.ts
var generateRuleAttributes = (rules) => (rules || []).map((rule) => {
  let values;
  const firstValue = Array.isArray(rule.values) ? rule.values[0]?.value : rule.values;
  if (rule.field_type === "number") {
    values = firstValue ? String(firstValue) : "";
  } else if (rule.operator === "eq") {
    values = firstValue ? String(firstValue) : "";
  } else {
    values = Array.isArray(rule.values) ? rule.values.map((v) => String(v.value || "")).filter(Boolean) : [];
  }
  return {
    id: rule.id,
    required: rule.required,
    field_type: rule.field_type,
    disguised: rule.disguised,
    attribute: rule.attribute || "",
    operator: rule.operator || "",
    values
  };
});

// src/pages/promotions/common/edit-rules/components/rule-value-form-field/rule-value-form-field.tsx
import { Input } from "@medusajs/ui";
import { useWatch } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useEffect, useRef } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
var buildFilters = (attribute, currencyCode) => {
  if (!attribute || !currencyCode) {
    return {};
  }
  if (attribute === "currency_code") {
    return {
      value: [currencyCode]
    };
  }
  return {};
};
var RuleValueFormField = ({
  form,
  identifier,
  scope,
  name,
  operator,
  fieldRule,
  attributes,
  ruleType,
  applicationMethodTargetType
}) => {
  const { t } = useTranslation();
  const attribute = attributes?.find(
    (attr) => attr.value === fieldRule.attribute
  );
  const { currency_code, isPending: isSellerLoading } = useCurrentSeller();
  const comboboxData = useComboboxData({
    queryFn: async (params) => {
      return await sdk.vendor.promotions.ruleValueOptions.$ruleType.$ruleAttributeId.query(
        {
          limit: params?.limit ?? 100,
          offset: params?.offset ?? 0,
          $ruleType: ruleType,
          $ruleAttributeId: attribute?.id,
          ...params,
          ...buildFilters(attribute?.id, currency_code),
          application_method_target_type: applicationMethodTargetType
        }
      );
    },
    enabled: !!attribute?.id && ["select", "multiselect"].includes(attribute.field_type) && !isSellerLoading,
    getOptions: (data) => data.values,
    queryKey: ["rule-value-options", ruleType, attribute?.id]
  });
  const watchOperator = useWatch({
    control: form.control,
    name: operator
  });
  const prevOperatorRef = useRef(watchOperator);
  useEffect(() => {
    if (prevOperatorRef.current === watchOperator) {
      return;
    }
    prevOperatorRef.current = watchOperator;
    if (watchOperator === "eq") {
      form.setValue(name, "");
    } else {
      form.setValue(name, []);
    }
  }, [
    watchOperator,
    form,
    name
  ]);
  return /* @__PURE__ */ jsx(
    Form.Field,
    {
      name,
      render: ({ field: { onChange, ref, ...field } }) => {
        if (attribute?.field_type === "number") {
          return /* @__PURE__ */ jsxs(Form.Item, { className: "basis-1/2", children: [
            /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
              Input,
              {
                ...field,
                type: "number",
                onChange,
                className: "bg-ui-bg-base",
                ref,
                min: 1,
                disabled: !fieldRule.attribute
              }
            ) }),
            /* @__PURE__ */ jsx(Form.ErrorMessage, {})
          ] });
        } else if (attribute?.field_type === "text") {
          return /* @__PURE__ */ jsxs(Form.Item, { className: "basis-1/2", children: [
            /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
              Input,
              {
                ...field,
                ref,
                onChange,
                className: "bg-ui-bg-base",
                disabled: !fieldRule.attribute
              }
            ) }),
            /* @__PURE__ */ jsx(Form.ErrorMessage, {})
          ] });
        } else {
          return /* @__PURE__ */ jsxs(Form.Item, { className: "basis-1/2", children: [
            /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
              Combobox,
              {
                ...field,
                ...comboboxData,
                ref,
                placeholder: watchOperator === "eq" ? t("labels.selectValue") : t("labels.selectValues"),
                disabled: !watchOperator,
                onChange
              }
            ) }),
            /* @__PURE__ */ jsx(Form.ErrorMessage, {})
          ] });
        }
      }
    },
    `${identifier}.${scope}.${name}-${fieldRule.attribute}`
  );
};

// src/pages/promotions/common/edit-rules/components/rules-form-field/constants.ts
var requiredProductRule = {
  id: "product",
  attribute: "items.product.id",
  attribute_label: "Product",
  operator: "eq",
  operator_label: "Equal",
  values: [],
  required: true,
  field_type: "select",
  disguised: false
};

// src/pages/promotions/common/edit-rules/components/rules-form-field/rules-form-field.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var RulesFormField = ({
  form,
  ruleType,
  setRulesToRemove,
  scope = "rules",
  promotion
}) => {
  const { t } = useTranslation2();
  const formData = form.getValues();
  const { attributes } = usePromotionRuleAttributes(ruleType, formData.type);
  const filteredAttributes = attributes?.filter(({ id }) => id === "country" || id === "product") || [];
  const { fields, append, remove, update, replace } = useFieldArray({
    control: form.control,
    name: scope,
    keyName: scope
  });
  const promotionType = useWatch2({
    control: form.control,
    name: "type",
    defaultValue: promotion?.type
  });
  const applicationMethodType = useWatch2({
    control: form.control,
    name: "application_method.type",
    defaultValue: promotion?.application_method?.type
  });
  const query = promotionType ? {
    promotion_type: promotionType,
    application_method_type: applicationMethodType
  } : {};
  const { rules, isLoading } = usePromotionRules(
    promotion?.id,
    ruleType,
    query,
    {
      enabled: !!promotion?.id || !!promotionType && !!applicationMethodType
    }
  );
  const rulesLoadedRef = useRef2(false);
  useEffect2(() => {
    if (isLoading) {
      return;
    }
    if (rulesLoadedRef.current) {
      return;
    }
    if (ruleType === "rules" && !fields.length) {
      form.resetField("rules");
      const formRules = generateRuleAttributes(rules);
      replace(formRules);
      rulesLoadedRef.current = true;
    }
    if (ruleType === "buy-rules" && !fields.length) {
      form.resetField("application_method.buy_rules");
      const apiRules = promotion?.id || promotionType === "standard" ? rules || [] : [...rules || [], requiredProductRule];
      const formRules = generateRuleAttributes(apiRules);
      replace(formRules);
      rulesLoadedRef.current = true;
    }
    if (ruleType === "target-rules" && !fields.length) {
      form.resetField("application_method.target_rules");
      const apiRules = promotion?.id || promotionType === "standard" ? rules || [] : [...rules || [], requiredProductRule];
      const formRules = generateRuleAttributes(apiRules);
      replace(formRules);
      rulesLoadedRef.current = true;
    }
  }, [
    promotionType,
    isLoading,
    ruleType,
    fields.length,
    form,
    replace,
    rules,
    promotion?.id
  ]);
  return /* @__PURE__ */ jsxs2("div", { className: "flex flex-col", children: [
    /* @__PURE__ */ jsx2(Heading, { level: "h2", className: "mb-2", children: t(`promotions.fields.conditions.${ruleType}.title`) }),
    /* @__PURE__ */ jsx2(Text, { className: "text-ui-fg-subtle txt-small mb-6", children: t(`promotions.fields.conditions.${ruleType}.description`) }),
    fields.map((fieldRule, index) => {
      const identifier = fieldRule.id;
      return /* @__PURE__ */ jsxs2(Fragment, { children: [
        /* @__PURE__ */ jsxs2("div", { className: "bg-ui-bg-subtle border-ui-border-base flex flex-row gap-2 rounded-xl border px-2 py-2", children: [
          /* @__PURE__ */ jsxs2("div", { className: "grow", children: [
            /* @__PURE__ */ jsx2(
              Form.Field,
              {
                name: `${scope}.${index}.attribute`,
                render: ({ field }) => {
                  const { onChange, ref, ...fieldProps } = field;
                  const existingAttributes = fields?.map((field2) => field2.attribute) || [];
                  const attributeOptions = filteredAttributes?.filter((attr) => {
                    if (attr.value === fieldRule.attribute) {
                      return true;
                    }
                    return !existingAttributes.includes(attr.value);
                  }) || [];
                  const disabled = !!fieldRule.required;
                  const onValueChange = (e) => {
                    const currentAttributeOption = attributeOptions.find(
                      (ao) => ao.id === e
                    );
                    update(index, {
                      ...fieldRule,
                      values: [],
                      disguised: currentAttributeOption?.disguised || false
                    });
                    onChange(e);
                  };
                  return /* @__PURE__ */ jsxs2(Form.Item, { className: "mb-2", children: [
                    fieldRule.required && /* @__PURE__ */ jsx2("div", { className: "flex items-center px-2", children: /* @__PURE__ */ jsx2("p", { className: "text text-ui-fg-muted txt-small", children: t("promotions.form.required") }) }),
                    /* @__PURE__ */ jsx2(Form.Control, { children: !disabled ? /* @__PURE__ */ jsxs2(
                      Select,
                      {
                        ...fieldProps,
                        onValueChange,
                        disabled: fieldRule.required,
                        children: [
                          /* @__PURE__ */ jsx2(
                            Select.Trigger,
                            {
                              ref,
                              className: "bg-ui-bg-base",
                              children: /* @__PURE__ */ jsx2(
                                Select.Value,
                                {
                                  placeholder: t(
                                    "promotions.form.selectAttribute"
                                  )
                                }
                              )
                            }
                          ),
                          /* @__PURE__ */ jsx2(Select.Content, { children: attributeOptions?.map((c, i) => /* @__PURE__ */ jsx2(
                            Select.Item,
                            {
                              value: c.value,
                              children: /* @__PURE__ */ jsx2("span", { className: "text-ui-fg-subtle", children: c.label })
                            },
                            `${identifier}-attribute-option-${i}`
                          )) })
                        ]
                      }
                    ) : /* @__PURE__ */ jsx2(
                      DisabledField,
                      {
                        label: attributeOptions?.find(
                          (ao) => ao.value === fieldRule.attribute
                        )?.label || "",
                        field
                      }
                    ) }),
                    /* @__PURE__ */ jsx2(Form.ErrorMessage, {})
                  ] });
                }
              }
            ),
            /* @__PURE__ */ jsxs2("div", { className: "flex gap-2", children: [
              /* @__PURE__ */ jsx2(
                Form.Field,
                {
                  name: `${scope}.${index}.operator`,
                  render: ({ field }) => {
                    const { onChange, ref, ...fieldProps } = field;
                    const currentAttributeOption = attributes?.find(
                      (attr) => attr.value === fieldRule.attribute
                    );
                    const options = currentAttributeOption?.operators?.map((o, idx) => ({
                      label: o.label,
                      value: o.value,
                      key: `${identifier}-operator-option-${idx}`
                    })) || [];
                    const disabled = !!fieldRule.attribute && options?.length <= 1;
                    return /* @__PURE__ */ jsxs2(Form.Item, { className: "basis-1/2", children: [
                      /* @__PURE__ */ jsx2(Form.Control, { children: !disabled ? /* @__PURE__ */ jsxs2(
                        Select,
                        {
                          ...fieldProps,
                          disabled: !fieldRule.attribute,
                          onValueChange: onChange,
                          children: [
                            /* @__PURE__ */ jsx2(
                              Select.Trigger,
                              {
                                ref,
                                className: "bg-ui-bg-base",
                                children: /* @__PURE__ */ jsx2(Select.Value, { placeholder: t("promotions.form.selectOperator") })
                              }
                            ),
                            /* @__PURE__ */ jsx2(Select.Content, { children: options?.map((c) => /* @__PURE__ */ jsx2(Select.Item, { value: c.value, children: /* @__PURE__ */ jsx2("span", { className: "text-ui-fg-subtle", children: c.label }) }, c.key)) })
                          ]
                        }
                      ) : /* @__PURE__ */ jsx2(
                        DisabledField,
                        {
                          label: options.find(
                            (o) => o.value === fieldProps.value
                          )?.label || "",
                          field
                        }
                      ) }),
                      /* @__PURE__ */ jsx2(Form.ErrorMessage, {})
                    ] });
                  }
                }
              ),
              /* @__PURE__ */ jsx2(
                RuleValueFormField,
                {
                  form,
                  identifier,
                  scope,
                  name: `${scope}.${index}.values`,
                  operator: `${scope}.${index}.operator`,
                  fieldRule,
                  attributes: attributes || [],
                  ruleType
                }
              )
            ] })
          ] }),
          /* @__PURE__ */ jsx2("div", { className: "size-7 flex-none self-center", children: !fieldRule.required && /* @__PURE__ */ jsx2(
            IconButton,
            {
              size: "small",
              variant: "transparent",
              className: "text-ui-fg-muted",
              type: "button",
              onClick: () => {
                if (!fieldRule.required) {
                  if (setRulesToRemove) {
                    setRulesToRemove(
                      (prev) => {
                        if (fieldRule.id && !prev.find((r) => r.id === fieldRule.id)) {
                          return [
                            ...prev || [],
                            fieldRule
                          ];
                        }
                        return prev || [];
                      }
                    );
                  }
                  remove(index);
                }
              },
              children: /* @__PURE__ */ jsx2(XMarkMini, {})
            }
          ) })
        ] }),
        index < fields.length - 1 && /* @__PURE__ */ jsxs2("div", { className: "relative px-6 py-3", children: [
          /* @__PURE__ */ jsx2("div", { className: "border-ui-border-strong absolute bottom-0 left-[40px] top-0 z-[-1] w-px bg-[linear-gradient(var(--border-strong)_33%,rgba(255,255,255,0)_0%)] bg-[length:1px_3px] bg-repeat-y" }),
          /* @__PURE__ */ jsx2(Badge, { size: "2xsmall", className: " text-xs", children: t("promotions.form.and") })
        ] })
      ] }, `${fieldRule.id}.${index}.${fieldRule.attribute}`);
    }),
    /* @__PURE__ */ jsxs2("div", { className: fields.length ? "mt-6" : "", children: [
      /* @__PURE__ */ jsx2(
        Button,
        {
          type: "button",
          variant: "secondary",
          className: "inline-block",
          disabled: fields.length >= filteredAttributes.length,
          onClick: () => {
            const newRule = {
              attribute: "",
              operator: "",
              values: [],
              required: false
            };
            append(newRule);
          },
          children: t("promotions.fields.addCondition")
        }
      ),
      !!fields.length && /* @__PURE__ */ jsx2(
        Button,
        {
          type: "button",
          variant: "transparent",
          className: "text-ui-fg-muted hover:text-ui-fg-subtle ml-2 inline-block",
          onClick: () => {
            const indicesToRemove = fields.map((field, index) => field.required ? null : index).filter((f) => f !== null);
            if (setRulesToRemove) {
              setRulesToRemove(
                fields.filter((field) => !field.required)
              );
            }
            remove(indicesToRemove);
          },
          children: t("promotions.fields.clearAll")
        }
      )
    ] })
  ] });
};
var DisabledField = forwardRef(
  ({ label, field }, ref) => {
    return /* @__PURE__ */ jsxs2("div", { children: [
      /* @__PURE__ */ jsx2("div", { className: "txt-compact-small bg-ui-bg-component shadow-borders-base text-ui-fg-base h-8 rounded-md px-2 py-1.5", children: label }),
      /* @__PURE__ */ jsx2("input", { ...field, ref, disabled: true, hidden: true })
    ] });
  }
);
DisabledField.displayName = "DisabledField";

export {
  RulesFormField
};
