"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 { ActionRow } from "./ActionRow.js";
import { validateActions } from "./utils/actionValidation.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { useConfirmDialog } from "@open-mercato/ui/backend/confirm-dialog";
function ActionBuilder({
  value,
  onChange,
  error,
  showJsonPreview = false,
  label,
  emptyMessage
}) {
  const t = useT();
  const { confirm: confirmDialog, ConfirmDialogElement } = useConfirmDialog();
  const [showDebug, setShowDebug] = React.useState(false);
  const actions = value || [];
  const handleAddAction = () => {
    const newAction = {
      type: "",
      config: {}
    };
    onChange([...actions, newAction]);
  };
  const handleChangeAction = (index, updatedAction) => {
    const newActions = [...actions];
    newActions[index] = updatedAction;
    onChange(newActions);
  };
  const handleDeleteAction = (index) => {
    const newActions = actions.filter((_, i) => i !== index);
    onChange(newActions);
  };
  const handleMoveUp = (index) => {
    if (index === 0) return;
    const newActions = [...actions];
    const temp = newActions[index - 1];
    newActions[index - 1] = newActions[index];
    newActions[index] = temp;
    onChange(newActions);
  };
  const handleMoveDown = (index) => {
    if (index === actions.length - 1) return;
    const newActions = [...actions];
    const temp = newActions[index + 1];
    newActions[index + 1] = newActions[index];
    newActions[index] = temp;
    onChange(newActions);
  };
  const handleClearAll = async () => {
    const confirmed = await confirmDialog({
      title: t("business_rules.components.actionBuilder.confirm.clearAll"),
      variant: "destructive"
    });
    if (confirmed) {
      onChange([]);
    }
  };
  const validation = React.useMemo(() => {
    return validateActions(actions, t);
  }, [actions, 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: label || t("business_rules.components.actionBuilder.label") }),
        actions.length > 0 && /* @__PURE__ */ jsxs("span", { className: "text-xs text-muted-foreground", children: [
          "(",
          t("business_rules.components.actionBuilder.actionCount", { count: actions.length }),
          ")"
        ] })
      ] }),
      /* @__PURE__ */ jsx("div", { className: "flex items-center gap-2", children: showJsonPreview && actions.length > 0 && /* @__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.actionBuilder.jsonPreview.toggle"),
          children: [
            /* @__PURE__ */ jsx(Code, { className: "w-3 h-3" }),
            showDebug ? t("business_rules.components.actionBuilder.jsonPreview.hide") : t("business_rules.components.actionBuilder.jsonPreview.show")
          ]
        }
      ) })
    ] }),
    actions.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: emptyMessage || t("business_rules.components.actionBuilder.emptyMessage") }),
      /* @__PURE__ */ jsxs(Button, { type: "button", onClick: handleAddAction, variant: "outline", size: "sm", children: [
        /* @__PURE__ */ jsx(Plus, { className: "w-4 h-4 mr-2" }),
        t("business_rules.components.actionBuilder.addAction")
      ] })
    ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx("div", { className: "space-y-2", children: actions.map((action, index) => /* @__PURE__ */ jsx(
        ActionRow,
        {
          action,
          index,
          onChange: handleChangeAction,
          onDelete: handleDeleteAction,
          onMoveUp: index > 0 ? handleMoveUp : void 0,
          onMoveDown: index < actions.length - 1 ? handleMoveDown : void 0,
          canMoveUp: index > 0,
          canMoveDown: index < actions.length - 1
        },
        index
      )) }),
      /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
        /* @__PURE__ */ jsxs(Button, { type: "button", onClick: handleAddAction, variant: "outline", size: "sm", children: [
          /* @__PURE__ */ jsx(Plus, { className: "w-4 h-4 mr-2" }),
          t("business_rules.components.actionBuilder.addAction")
        ] }),
        actions.length > 1 && /* @__PURE__ */ jsx(Button, { type: "button", onClick: handleClearAll, variant: "outline", size: "sm", className: "text-status-error-text", children: t("business_rules.components.actionBuilder.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.actionBuilder.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 && actions.length > 0 && /* @__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(actions, null, 2) }) }),
    actions.length > 0 && /* @__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.actionBuilder.help.actionOrder") }),
        " ",
        t("business_rules.components.actionBuilder.help.actionOrderDescription")
      ] }),
      /* @__PURE__ */ jsxs("p", { children: [
        /* @__PURE__ */ jsx("strong", { children: t("business_rules.components.actionBuilder.help.messageInterpolation") }),
        " ",
        t("business_rules.components.actionBuilder.help.messageInterpolationDescription")
      ] })
    ] }),
    ConfirmDialogElement
  ] });
}
export {
  ActionBuilder
};
//# sourceMappingURL=ActionBuilder.js.map
