"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.Suggestions = exports.SuggestionItem = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _designSystem = require("@wix/design-system");
var _SuggestionsState = require("../../state/Suggestions/SuggestionsState");
var _mobxReactLite = require("mobx-react-lite");
var _react2 = require("@wix/bex-core/react");
var _MarketingCard = require("./MarketingCard");
var _VisibilityTrackedCard = require("./VisibilityTrackedCard");
var _SuggestionsContext = require("./SuggestionsContext");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/Suggestions/Suggestions.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const defaultCarouselProps = {
  hideDots: true,
  infinite: false,
  variableWidth: false,
  controlsPosition: 'overlay',
  showControlsShadow: true,
  sidesGradientColor: _designSystem.COLOR_TOKENS['--wsr-color-D70'],
  controlsStartEnd: 'hidden'
};
const SuggestionsContainer = ({
  count,
  children,
  carouselProps,
  dataHook,
  dataState,
  maxInlineItems
}) => {
  if (count <= maxInlineItems) {
    return /*#__PURE__*/_react.default.createElement("div", {
      "data-hook": dataHook,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 56,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
      direction: "horizontal",
      gap: "SP4",
      dataHook: `suggestions-container-${dataState}`,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 57,
        columnNumber: 9
      }
    }, children));
  }
  return /*#__PURE__*/_react.default.createElement("div", {
    "data-hook": dataHook,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 69,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.CarouselWIP, (0, _extends2.default)({
    dataHook: `suggestions-carousel-${dataState}`
  }, defaultCarouselProps, carouselProps, {
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 70,
      columnNumber: 7
    }
  }), children));
};
const SuggestionItem = ({
  children,
  dataId
}) => /*#__PURE__*/_react.default.createElement("div", {
  "data-hook": "suggestion-item",
  "data-id": dataId,
  style: {
    display: 'contents'
  },
  __self: void 0,
  __source: {
    fileName: _jsxFileName,
    lineNumber: 85,
    columnNumber: 3
  }
}, children);
exports.SuggestionItem = SuggestionItem;
const Suggestions = exports.Suggestions = (0, _mobxReactLite.observer)(({
  placementId,
  renderCard,
  CardSkeleton = _MarketingCard.MarketingCardSkeleton,
  carouselProps,
  skeletonsNumber = 4,
  dataHook = 'suggestions',
  maxInlineItems = 2
}) => {
  const container = (0, _react2.useWixPatternsContainer)();
  const [state] = (0, _react.useState)(() => new _SuggestionsState.SuggestionsState({
    placementId,
    container
  }));
  (0, _react.useEffect)(() => {
    return state.init();
  }, [state]);
  if (state.hasOffers) {
    // use initial cards count for container, to match the count the cards get rendered with
    const cardCount = state.offers.length;
    const cards = state.offers.map((offer, index) => {
      const key = offer.offerGuid || `suggestion-${index}`;
      return {
        key,
        offerGuid: offer.offerGuid,
        element: renderCard({
          offer,
          index,
          totalOffers: cardCount
        })
      };
    }).filter(card => card.element != null);
    if (cards.length === 0) {
      return null;
    }
    return /*#__PURE__*/_react.default.createElement(_SuggestionsContext.SuggestionsProvider, {
      onOfferClicked: state.onOfferClicked,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 140,
        columnNumber: 9
      }
    }, /*#__PURE__*/_react.default.createElement(SuggestionsContainer, {
      count: cardCount,
      carouselProps: carouselProps,
      dataHook: dataHook,
      dataState: "idle",
      maxInlineItems: maxInlineItems,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 141,
        columnNumber: 11
      }
    }, cards.map(({
      key,
      offerGuid,
      element
    }) => /*#__PURE__*/_react.default.createElement(SuggestionItem, {
      key: key,
      dataId: key,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 149,
        columnNumber: 15
      }
    }, /*#__PURE__*/_react.default.createElement(_VisibilityTrackedCard.VisibilityTrackedCard, {
      itemKey: key,
      element: element,
      onObserved: () => offerGuid && state.onOfferViewed(offerGuid),
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 150,
        columnNumber: 17
      }
    })))));
  }
  if (state.isFetching) {
    const skeletons = Array.from({
      length: skeletonsNumber
    }).map((_, index) => /*#__PURE__*/_react.default.createElement(SuggestionItem, {
      key: index,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 165,
        columnNumber: 11
      }
    }, /*#__PURE__*/_react.default.createElement(CardSkeleton, {
      index: index,
      totalOffers: skeletonsNumber,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 166,
        columnNumber: 13
      }
    })));
    return /*#__PURE__*/_react.default.createElement(_SuggestionsContext.SuggestionsProvider, {
      onOfferClicked: state.onOfferClicked,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 172,
        columnNumber: 9
      }
    }, /*#__PURE__*/_react.default.createElement(SuggestionsContainer, {
      count: skeletonsNumber,
      carouselProps: {
        ...carouselProps,
        controlsPosition: 'none'
      },
      dataHook: dataHook,
      dataState: "loading",
      maxInlineItems: maxInlineItems,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 173,
        columnNumber: 11
      }
    }, skeletons));
  }
  return null;
});
//# sourceMappingURL=Suggestions.js.map