"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var assistant_timeline_exports = {};
__export(assistant_timeline_exports, {
  MessageTimeline: () => MessageTimeline
});
module.exports = __toCommonJS(assistant_timeline_exports);
var import_design_system = require("@wix/design-system");
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_uncreachable = require("./../../_shared/uncreachable.js");
var import_assistant_error_descriptor = require("./assistant-error-descriptor.js");
var import_assistant_timeline_scss = __toESM(require("./assistant-timeline-scss.js"));
const MAX_DESCRIPTION_LENGTH = 60;
const ExpandableText = ({ text, className }) => {
  const [expanded, setExpanded] = (0, import_react.useState)(false);
  const needsTruncation = text.length > MAX_DESCRIPTION_LENGTH;
  const displayText = !expanded && needsTruncation ? text.slice(0, MAX_DESCRIPTION_LENGTH).trimEnd() + "…" : text;
  return /* @__PURE__ */ import_react.default.createElement(
    import_design_system.Text,
    {
      className,
      variant: "normal",
      size: "small",
      onClick: needsTruncation ? () => setExpanded((prev) => !prev) : void 0,
      style: needsTruncation ? { cursor: "pointer" } : void 0
    },
    displayText
  );
};
const renderStepIcon = (status) => {
  switch (status) {
    case "success":
      return /* @__PURE__ */ import_react.default.createElement(import_wix_ui_icons_common.Confirm, { className: import_assistant_timeline_scss.default.stepIcon });
    case "inProgress":
      return /* @__PURE__ */ import_react.default.createElement("div", { className: import_assistant_timeline_scss.default.loaderContainer }, /* @__PURE__ */ import_react.default.createElement(import_design_system.Loader, { size: "tiny" }));
    case "error":
      return /* @__PURE__ */ import_react.default.createElement(import_wix_ui_icons_common.StatusAlertSmall, { className: import_assistant_timeline_scss.default.stepIcon });
    case "pending":
      return /* @__PURE__ */ import_react.default.createElement(import_wix_ui_icons_common.CircleSmallFilledSmall, { className: import_assistant_timeline_scss.default.stepIcon });
    default:
      throw (0, import_uncreachable.unreachable)(status);
  }
};
const getTimelineStepId = (task, index) => task.id ?? `step-${index}-${task.message}`;
const MessageTimeline = ({
  title,
  subtitle,
  tasks,
  isLoading = false,
  onRetry,
  footer,
  t
}) => {
  const visibleSteps = tasks ?? [];
  const expandedStepsResetKey = visibleSteps.map((task, index) => `${getTimelineStepId(task, index)}:${task.status}`).join("|");
  const [expandedSteps, setExpandedSteps] = (0, import_react.useState)({});
  (0, import_react.useEffect)(() => {
    const expanded = {};
    visibleSteps.forEach((task, index) => {
      expanded[getTimelineStepId(task, index)] = task.status === "inProgress";
    });
    setExpandedSteps(expanded);
  }, [expandedStepsResetKey]);
  const toggleStep = (stepId) => {
    setExpandedSteps((prev) => ({
      ...prev,
      [stepId]: !prev[stepId]
    }));
  };
  if (visibleSteps.some((step) => step.status === "error")) {
    const errorStep = visibleSteps.find((step) => step.status === "error");
    const errorDescriptor = (0, import_assistant_error_descriptor.getAssistantErrorDescriptor)({
      applicationErrorCode: errorStep?.applicationErrorCode,
      message: errorStep?.description?.[0] ?? errorStep?.message,
      t
    });
    return /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { className: import_assistant_timeline_scss.default.assistantTimelineErrorContainer, direction: "vertical" }, /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { verticalAlign: "middle", marginBottom: "8px" }, /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { marginRight: "8px", color: "#D6453D" }, /* @__PURE__ */ import_react.default.createElement(import_wix_ui_icons_common.StatusAlertSmall, { size: "24" })), /* @__PURE__ */ import_react.default.createElement(import_design_system.Text, { size: "small", weight: "bold" }, errorDescriptor.title)), /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { marginBottom: "16px" }, /* @__PURE__ */ import_react.default.createElement(import_design_system.Text, { size: "small" }, errorDescriptor.subtitle)), onRetry && errorDescriptor.isRetriable && /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(import_design_system.Button, { size: "small", priority: "primary", onClick: onRetry, skin: "dark" }, t("AssistantTimeline_Button_TryAgain"))));
  }
  return /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { className: import_assistant_timeline_scss.default.assistantTimelineContainer, direction: "vertical" }, /* @__PURE__ */ import_react.default.createElement(import_design_system.Text, { variant: "normal", size: "small", weight: "bold" }, title), /* @__PURE__ */ import_react.default.createElement(import_design_system.Text, { className: import_assistant_timeline_scss.default.timelineSubtitle, variant: "normal", size: "small" }, subtitle), /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { className: import_assistant_timeline_scss.default.stepsContainer, direction: "vertical" }, isLoading ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_design_system.SkeletonGroup, null, /* @__PURE__ */ import_react.default.createElement(import_design_system.SkeletonLine, { width: "75%", marginBottom: "8px" }), /* @__PURE__ */ import_react.default.createElement(import_design_system.SkeletonLine, { width: "60%", marginBottom: "16px" })), /* @__PURE__ */ import_react.default.createElement(import_design_system.SkeletonGroup, null, /* @__PURE__ */ import_react.default.createElement(import_design_system.SkeletonLine, { width: "80%", marginBottom: "8px" }), /* @__PURE__ */ import_react.default.createElement(import_design_system.SkeletonLine, { width: "55%", marginBottom: "16px" }))) : visibleSteps.map((step, index) => {
    const stepId = getTimelineStepId(step, index);
    return /* @__PURE__ */ import_react.default.createElement(
      import_design_system.Box,
      {
        key: stepId,
        className: (0, import_classnames.default)(import_assistant_timeline_scss.default.stepItem, {
          [import_assistant_timeline_scss.default.stepItemLoading]: step.status === "inProgress"
        }),
        direction: "vertical"
      },
      /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          onClick: () => toggleStep(stepId),
          onKeyDown: (e) => {
            if (e.key === "Enter" || e.key === " ") {
              e.preventDefault();
              toggleStep(stepId);
            }
          },
          role: "button",
          tabIndex: 0
        },
        /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { className: import_assistant_timeline_scss.default.stepHeader }, renderStepIcon(step.status), /* @__PURE__ */ import_react.default.createElement(import_design_system.Text, { className: import_assistant_timeline_scss.default.stepTitle, variant: "normal", size: "small" }, step.message), /* @__PURE__ */ import_react.default.createElement(
          import_wix_ui_icons_common.ChevronDown,
          {
            visibility: step.description && step.description.length > 0 ? "visible" : "hidden",
            className: `${import_assistant_timeline_scss.default.chevronIcon} ${expandedSteps[stepId] ? import_assistant_timeline_scss.default.expanded : ""}`
          }
        ))
      ),
      /* @__PURE__ */ import_react.default.createElement(import_design_system.Collapse, { open: !!expandedSteps[stepId] }, /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { direction: "vertical", className: import_assistant_timeline_scss.default.stepDetails }, step.description?.map((desc, descriptionIndex) => /* @__PURE__ */ import_react.default.createElement(
        ExpandableText,
        {
          key: descriptionIndex,
          text: desc,
          className: (0, import_classnames.default)(
            import_assistant_timeline_scss.default.timelineSubtitle,
            step.isToolDescription && import_assistant_timeline_scss.default.toolDescription
          )
        }
      ))))
    );
  })), footer && /* @__PURE__ */ import_react.default.createElement("div", { className: import_assistant_timeline_scss.default.timelineFooter }, footer));
};
//# sourceMappingURL=assistant-timeline.js.map
