"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Button } from "@open-mercato/ui/primitives/button";
import { Plus, Code } from "lucide-react";
import { ConditionGroup } from "./ConditionGroup.js";
import { validateConditionExpression } from "./utils/conditionValidation.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { useConfirmDialog } from "@open-mercato/ui/backend/confirm-dialog";
function ConditionBuilder({
  value,
  onChangeAction,
  entityType,
  maxDepth = 5,
  error,
  showJsonPreview = false
}) {
  const t = useT();
  const { confirm: confirmDialog, ConfirmDialogElement } = useConfirmDialog();
  const [showDebug, setShowDebug] = React.useState(false);
  const handleInitialize = () => {
    const initialGroup = {
      operator: "AND",
      rules: [
        {
          field: "",
          operator: "=",
          value: null
        }
      ]
    };
    onChangeAction(initialGroup);
  };
  const handleChange = (updatedGroup) => {
    onChangeAction(updatedGroup);
  };
  const handleClear = async () => {
    const confirmed = await confirmDialog({
      title: t("business_rules.components.conditionBuilder.confirm.clearAll"),
      variant: "destructive"
    });
    if (confirmed) {
      onChangeAction({
        operator: "AND",
        rules: []
      });
    }
  };
  const validation = React.useMemo(() => {
    return value ? validateConditionExpression(value, 0, 5, t) : { valid: true, errors: [] };
  }, [value, t]);
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
      /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
        /* @__PURE__ */ jsx("h3", { className: "text-sm font-medium text-foreground", children: t("business_rules.components.conditionBuilder.title") }),
        value && value.rules && value.rules.length > 0 && /* @__PURE__ */ jsxs("span", { className: "text-xs text-muted-foreground", children: [
          "(",
          t("business_rules.components.conditionBuilder.ruleCount", { count: value.rules.length }),
          ")"
        ] })
      ] }),
      showJsonPreview && value && /* @__PURE__ */ jsxs(
        "button",
        {
          type: "button",
          onClick: () => setShowDebug(!showDebug),
          className: "flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground",
          title: t("business_rules.components.conditionBuilder.jsonPreview.toggle"),
          children: [
            /* @__PURE__ */ jsx(Code, { className: "w-3 h-3" }),
            showDebug ? t("business_rules.components.conditionBuilder.jsonPreview.hide") : t("business_rules.components.conditionBuilder.jsonPreview.show")
          ]
        }
      )
    ] }),
    !value || !value.rules || value.rules.length === 0 ? /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center justify-center p-8 border-2 border-dashed border-border rounded-lg bg-muted", children: [
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground mb-4", children: t("business_rules.components.conditionBuilder.emptyMessage") }),
      /* @__PURE__ */ jsxs(Button, { type: "button", onClick: handleInitialize, variant: "outline", size: "sm", children: [
        /* @__PURE__ */ jsx(Plus, { className: "w-4 h-4 mr-2" }),
        t("business_rules.components.conditionBuilder.addFirstCondition")
      ] })
    ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(
        ConditionGroup,
        {
          group: value,
          onChange: handleChange,
          depth: 0,
          maxDepth,
          entityType
        }
      ),
      /* @__PURE__ */ jsx("div", { className: "flex justify-end", children: /* @__PURE__ */ jsx(Button, { type: "button", onClick: handleClear, variant: "outline", size: "sm", className: "text-status-error-text", children: t("business_rules.components.conditionBuilder.clearAll") }) })
    ] }),
    !validation.valid && /* @__PURE__ */ jsxs("div", { className: "p-3 bg-status-error-bg border border-status-error-border rounded", children: [
      /* @__PURE__ */ jsx("p", { className: "text-sm font-medium text-status-error-text mb-1", children: t("business_rules.components.conditionBuilder.validationErrors") }),
      /* @__PURE__ */ jsx("ul", { className: "list-disc list-inside space-y-0.5", children: validation.errors.map((err, index) => /* @__PURE__ */ jsx("li", { className: "text-xs text-status-error-text", children: err }, index)) })
    ] }),
    error && /* @__PURE__ */ jsx("div", { className: "p-3 bg-status-error-bg border border-status-error-border rounded", children: /* @__PURE__ */ jsx("p", { className: "text-sm text-status-error-text", children: error }) }),
    showDebug && value && /* @__PURE__ */ jsx("div", { className: "p-3 bg-zinc-900 dark:bg-zinc-950 rounded text-xs font-mono overflow-x-auto border border-border", children: /* @__PURE__ */ jsx("pre", { className: "text-zinc-100", children: JSON.stringify(value, null, 2) }) }),
    /* @__PURE__ */ jsxs("div", { className: "text-xs text-muted-foreground space-y-1", children: [
      /* @__PURE__ */ jsxs("p", { children: [
        /* @__PURE__ */ jsx("strong", { children: t("business_rules.components.conditionBuilder.help.fieldPaths") }),
        " ",
        t("business_rules.components.conditionBuilder.help.fieldPathsDescription")
      ] }),
      /* @__PURE__ */ jsxs("p", { children: [
        /* @__PURE__ */ jsx("strong", { children: t("business_rules.components.conditionBuilder.help.values") }),
        " ",
        t("business_rules.components.conditionBuilder.help.valuesDescription")
      ] }),
      /* @__PURE__ */ jsxs("p", { children: [
        /* @__PURE__ */ jsx("strong", { children: t("business_rules.components.conditionBuilder.help.fieldComparison") }),
        " ",
        t("business_rules.components.conditionBuilder.help.fieldComparisonDescription")
      ] })
    ] }),
    ConfirmDialogElement
  ] });
}
export {
  ConditionBuilder
};
//# sourceMappingURL=ConditionBuilder.js.map
