"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 variant_picker_exports = {};
__export(variant_picker_exports, {
  SMART_BLOCK_VARIANT_PICKER_MODAL_ID: () => SMART_BLOCK_VARIANT_PICKER_MODAL_ID,
  VariantPicker: () => VariantPicker
});
module.exports = __toCommonJS(variant_picker_exports);
var import_design_system = require("@wix/design-system");
var import_react = __toESM(require("react"));
var import_context = require("./../../pm-converters/context.js");
var import_ricos_context = require("./../../ricos-context/index.js");
var import_modal_service_provider = require("./../../ricos-modals/modal-service-provider.js");
var import_pm_node_types = require("./../../_shared/pm-node-types.js");
var import_ui_components = require("./../../ui-components/index.js");
var import_helpers = require("./../../tiptap-editor/helpers/index.js");
var import_consts = require("../consts.js");
var import_data_hooks = require("../data-hooks.js");
var import_create_boxes_template = require("../templates/create-boxes-template.js");
var import_variant_features = require("../utils/variant-features.js");
var import_alternating = require("./icons/alternating.js");
var import_arrow_bullets = require("./icons/arrow-bullets.js");
var import_bar_stats = require("./icons/bar-stats.js");
var import_big_bullets = require("./icons/big-bullets.js");
var import_circle_stats = require("./icons/circle-stats.js");
var import_circle = require("./icons/circle.js");
var import_joined = require("./icons/joined.js");
var import_minimal_timeline = require("./icons/minimal-timeline.js");
var import_numbered = require("./icons/numbered.js");
var import_pills = require("./icons/pills.js");
var import_process_steps = require("./icons/process-steps.js");
var import_pyramid = require("./icons/pyramid.js");
var import_quote_boxes = require("./icons/quote-boxes.js");
var import_side_line = require("./icons/side-line.js");
var import_small_bullets = require("./icons/small-bullets.js");
var import_solid = require("./icons/solid.js");
var import_staircase = require("./icons/staircase.js");
var import_star_rating = require("./icons/star-rating.js");
var import_stats = require("./icons/stats.js");
var import_timeline = require("./icons/timeline.js");
var import_top_circle = require("./icons/top-circle.js");
var import_top_line = require("./icons/top-line.js");
var import_vertical_funnel = require("./icons/vertical-funnel.js");
var import_variant_picker_scss = __toESM(require("./variant-picker-scss.js"));
const SMART_BLOCK_VARIANT_PICKER_MODAL_ID = "smart-block-insert-modal";
const VARIANT_SECTIONS = [
  {
    title: "SmartBlock_VariantSection_Boxes",
    variants: [
      { id: "SOLID_BOXES", label: "SmartBlock_Variant_Solid", icon: import_solid.SolidIcon },
      { id: "SOLID_JOINED_BOXES", label: "SmartBlock_Variant_Joined", icon: import_joined.JoinedIcon },
      { id: "NUMBERED_BOXES", label: "SmartBlock_Variant_Numbered", icon: import_numbered.NumberedIcon },
      {
        id: "SOLID_BOXES_ALTERNATING",
        label: "SmartBlock_Variant_Alternating",
        icon: import_alternating.AlternatingIcon
      },
      { id: "QUOTE_BOXES", label: "SmartBlock_Variant_QuoteBoxes", icon: import_quote_boxes.QuoteBoxesIcon }
    ]
  },
  {
    title: "SmartBlock_VariantSection_Lines",
    variants: [
      { id: "SIDE_LINE_TEXT", label: "SmartBlock_Variant_SideLine", icon: import_side_line.SideLineIcon },
      { id: "TOP_LINE_TEXT", label: "SmartBlock_Variant_TopLine", icon: import_top_line.TopLineIcon },
      { id: "PROCESS_STEPS", label: "SmartBlock_Variant_ProcessSteps", icon: import_process_steps.ProcessStepsIcon },
      {
        id: "OUTLINE_BOXES_WITH_TOP_CIRCLE",
        label: "SmartBlock_Variant_TopCircle",
        icon: import_top_circle.TopCircleIcon
      }
    ]
  },
  {
    title: "SmartBlock_VariantSection_Bullets",
    variants: [
      { id: "SMALL_BULLETS", label: "SmartBlock_Variant_SmallBullets", icon: import_small_bullets.SmallBulletsIcon },
      { id: "BIG_BULLETS", label: "SmartBlock_Variant_BigBullets", icon: import_big_bullets.BigBulletsIcon },
      { id: "ARROW_BULLETS", label: "SmartBlock_Variant_ArrowBullets", icon: import_arrow_bullets.ArrowBulletsIcon }
    ]
  },
  {
    title: "SmartBlock_VariantSection_Stats",
    variants: [
      { id: "STATS", label: "SmartBlock_Variant_Stats", icon: import_stats.StatsIcon },
      { id: "BAR_STATS", label: "SmartBlock_Variant_BarStats", icon: import_bar_stats.BarStatsIcon },
      { id: "CIRCLE_STATS", label: "SmartBlock_Variant_CircleStats", icon: import_circle_stats.CircleStatsIcon },
      { id: "STAR_RATING", label: "SmartBlock_Variant_StarRating", icon: import_star_rating.StarRatingIcon }
    ]
  },
  {
    title: "SmartBlock_VariantSection_Sequence",
    variants: [
      { id: "TIMELINE", label: "SmartBlock_Variant_Timeline", icon: import_timeline.TimelineIcon },
      {
        id: "MINIMAL_TIMELINE",
        label: "SmartBlock_Variant_MinimalTimeline",
        icon: import_minimal_timeline.MinimalTimelineIcon
      },
      { id: "PILLS", label: "SmartBlock_Variant_Pills", icon: import_pills.PillsIcon }
    ]
  },
  {
    title: "SmartBlock_VariantSection_Steps",
    variants: [
      { id: "PYRAMID", label: "SmartBlock_Variant_Pyramid", icon: import_pyramid.PyramidIcon },
      {
        id: "VERTICAL_FUNNEL",
        label: "SmartBlock_Variant_VerticalFunnel",
        icon: import_vertical_funnel.VerticalFunnelIcon
      },
      { id: "STAIRCASE", label: "SmartBlock_Variant_Staircase", icon: import_staircase.StaircaseIcon },
      { id: "CIRCLE", label: "SmartBlock_Variant_Circle", icon: import_circle.CircleIcon }
    ]
  }
];
const VariantPicker = ({ onSelect, currentType, nodeId }) => {
  const { t, isMobile } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const { adapter } = (0, import_react.useContext)(import_ricos_context.EditorContext);
  const modals = (0, import_modal_service_provider.useModalService)();
  const converters = (0, import_context.useConverters)();
  const [searchValue, setSearchValue] = (0, import_react.useState)("");
  const enterTimerRef = (0, import_react.useRef)(null);
  const leaveTimerRef = (0, import_react.useRef)(null);
  const previewingRef = (0, import_react.useRef)(false);
  const originalTypeRef = (0, import_react.useRef)(currentType);
  const selectedRef = (0, import_react.useRef)(null);
  const onSelectRef = (0, import_react.useRef)(onSelect);
  onSelectRef.current = onSelect;
  (0, import_react.useEffect)(() => {
    selectedRef.current?.scrollIntoView({ block: "center" });
  }, []);
  (0, import_react.useEffect)(
    () => () => {
      if (enterTimerRef.current) {
        clearTimeout(enterTimerRef.current);
      }
      if (leaveTimerRef.current) {
        clearTimeout(leaveTimerRef.current);
      }
      if (previewingRef.current && originalTypeRef.current && onSelectRef.current) {
        onSelectRef.current(originalTypeRef.current, true);
        previewingRef.current = false;
      }
    },
    []
  );
  const closeModal = () => modals?.closeModal(SMART_BLOCK_VARIANT_PICKER_MODAL_ID);
  const handleHoverEnter = (type) => {
    if (!onSelect) {
      return;
    }
    if (leaveTimerRef.current) {
      clearTimeout(leaveTimerRef.current);
      leaveTimerRef.current = null;
    }
    if (enterTimerRef.current) {
      clearTimeout(enterTimerRef.current);
    }
    enterTimerRef.current = setTimeout(() => {
      enterTimerRef.current = null;
      previewingRef.current = true;
      onSelect(type, true);
    }, 300);
  };
  const handleHoverLeave = () => {
    if (enterTimerRef.current) {
      clearTimeout(enterTimerRef.current);
      enterTimerRef.current = null;
    }
    if (previewingRef.current && originalTypeRef.current && onSelect) {
      leaveTimerRef.current = setTimeout(() => {
        leaveTimerRef.current = null;
        previewingRef.current = false;
        onSelect(originalTypeRef.current, true);
      }, 300);
    }
  };
  const translatedSections = (0, import_react.useMemo)(
    () => VARIANT_SECTIONS.map((s) => ({
      ...s,
      title: t(s.title),
      variants: s.variants.map((v) => ({ ...v, label: t(v.label) }))
    })),
    [t]
  );
  const allTranslatedVariants = (0, import_react.useMemo)(
    () => translatedSections.flatMap((s) => s.variants),
    [translatedSections]
  );
  const previewMap = (0, import_react.useMemo)(() => {
    let baseNodeJson;
    if (nodeId) {
      const tr = adapter.tiptapEditor.view.state.tr;
      const nodeWithPos = (0, import_helpers.findNodeById)(tr, nodeId)?.[0];
      baseNodeJson = nodeWithPos?.node.toJSON();
    }
    return new Map(
      allTranslatedVariants.map(({ id, label, icon }) => [
        id,
        {
          id,
          label,
          icon,
          content: baseNodeJson ? converters.fromPm({
            content: [applyTypeToNode(baseNodeJson, id)]
          }) : (0, import_create_boxes_template.createSmartBlockTemplate)(id, t)
        }
      ])
    );
  }, [nodeId, t, adapter, converters, allTranslatedVariants]);
  const filteredVariants = searchValue ? allTranslatedVariants.filter((v) => v.label.toLowerCase().includes(searchValue.toLowerCase())) : [];
  const handleSelect = (type) => {
    if (enterTimerRef.current) {
      clearTimeout(enterTimerRef.current);
      enterTimerRef.current = null;
    }
    if (leaveTimerRef.current) {
      clearTimeout(leaveTimerRef.current);
      leaveTimerRef.current = null;
    }
    previewingRef.current = false;
    if (onSelect) {
      onSelect(type);
    } else {
      const content = (0, import_create_boxes_template.createSmartBlockTemplate)(type, t);
      adapter.tiptapEditor.commands.insertSmartBlockPreset(content);
    }
    closeModal();
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_ui_components.Panel,
    {
      dataHook: import_data_hooks.DATA_HOOKS.INSERT_MODAL,
      closeButtonProps: { onClick: closeModal },
      maxHeight: isMobile ? void 0 : 576
    },
    /* @__PURE__ */ import_react.default.createElement(import_ui_components.PanelHeader, { title: t("SmartBlock_Panel_Title") }),
    /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { paddingLeft: "SP4", paddingRight: "SP4", paddingBottom: "SP3", paddingTop: "SP3" }, /* @__PURE__ */ import_react.default.createElement(
      import_design_system.Search,
      {
        value: searchValue,
        onChange: (e) => setSearchValue(e.target.value),
        onClear: () => setSearchValue(""),
        placeholder: t("SmartBlock_VariantPicker_SearchPlaceholder"),
        size: "small"
      }
    )),
    !searchValue && /* @__PURE__ */ import_react.default.createElement(import_design_system.Divider, null),
    /* @__PURE__ */ import_react.default.createElement(
      import_design_system.Box,
      {
        overflowY: "auto",
        direction: "vertical",
        paddingBottom: "SP4",
        style: { flex: 1, minHeight: 0 },
        className: import_variant_picker_scss.default.variantsBox
      },
      searchValue ? /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { paddingLeft: "SP4", paddingRight: "SP4", paddingTop: "SP1" }, /* @__PURE__ */ import_react.default.createElement("div", { className: import_variant_picker_scss.default.variantContainer }, filteredVariants.map((v) => {
        const variant = previewMap.get(v.id);
        return /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            key: variant.id,
            ref: variant.id === currentType ? selectedRef : void 0,
            onMouseOver: () => handleHoverEnter(variant.id),
            onMouseLeave: handleHoverLeave
          },
          /* @__PURE__ */ import_react.default.createElement(
            LayoutThumbnail,
            {
              image: /* @__PURE__ */ import_react.default.createElement(variant.icon, null),
              title: t(variant.label),
              selected: variant.id === currentType,
              onClick: () => handleSelect(variant.id)
            }
          )
        );
      }))) : /* @__PURE__ */ import_react.default.createElement(
        import_design_system.Accordion,
        {
          hideShadow: true,
          skin: "light",
          size: "medium",
          multiple: true,
          dataHook: "variant-accordion",
          items: translatedSections.map(
            (section) => (0, import_design_system.accordionItemBuilder)({
              initiallyOpen: true,
              title: /* @__PURE__ */ import_react.default.createElement(import_design_system.Text, { size: "small", weight: "bold" }, section.title),
              children: /* @__PURE__ */ import_react.default.createElement("div", { className: import_variant_picker_scss.default.variantContainer }, section.variants.map((v) => {
                const variant = previewMap.get(v.id);
                return /* @__PURE__ */ import_react.default.createElement(
                  "div",
                  {
                    key: variant.id,
                    ref: variant.id === currentType ? selectedRef : void 0,
                    onMouseOver: () => handleHoverEnter(variant.id),
                    onMouseLeave: handleHoverLeave
                  },
                  /* @__PURE__ */ import_react.default.createElement(
                    LayoutThumbnail,
                    {
                      image: /* @__PURE__ */ import_react.default.createElement(variant.icon, null),
                      title: t(variant.label),
                      selected: variant.id === currentType,
                      onClick: () => handleSelect(variant.id)
                    }
                  )
                );
              }))
            })
          )
        }
      )
    )
  );
};
const LayoutThumbnail = (props) => {
  return /* @__PURE__ */ import_react.default.createElement(
    import_design_system.Thumbnail,
    {
      height: 120,
      width: 117,
      size: "tiny",
      contentAlignment: "center",
      textPosition: "outside",
      ...props
    }
  );
};
const applyTypeToNode = (pmJson, type) => {
  const oldType = pmJson.attrs?.type;
  const result = { ...pmJson, attrs: { ...pmJson.attrs, type } };
  if (result.content) {
    result.content = result.content.map((cell) => {
      if (cell.type !== import_pm_node_types.PmNodeType.SMART_BLOCK_CELL) {
        return cell;
      }
      const newColorAttr = (0, import_variant_features.getCellColorAttr)(type);
      const newAttrs = { ...cell.attrs, type };
      if (oldType) {
        const oldColorAttr = (0, import_variant_features.getCellColorAttr)(oldType);
        if (oldColorAttr !== newColorAttr) {
          newAttrs[newColorAttr] = cell.attrs?.[oldColorAttr];
          newAttrs[oldColorAttr] = null;
        }
      }
      const isStatVariant = type === "CIRCLE_STATS" || type === "BAR_STATS" || type === "STATS";
      if (isStatVariant && !cell.attrs?.label) {
        newAttrs.label = import_consts.DEFAULT_LABEL;
      } else if (type === "STAR_RATING" && !cell.attrs?.label) {
        newAttrs.label = import_consts.DEFAULT_STAR_RATING_LABEL;
      }
      return { ...cell, attrs: newAttrs };
    });
  }
  return result;
};
//# sourceMappingURL=variant-picker.js.map
