/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("./index.compiled.css");
var _runtime = require("@compiled/react/runtime");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _compiled = require("@atlaskit/primitives/compiled");
var _CarouselSlide = _interopRequireDefault(require("./CarouselSlide"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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); }
var styles = {
  carousel: "_1reo15vq _18m915vq _1e0c1txw _1bsb1osq _4t3i1osq _kqswh2mm"
};

// Slide enters from the right: translates from 40px right + fades in
var slideInFromRight = null;
var slideWrapperMap = {
  static: "_1bsb1osq _4t3i1osq",
  animated: "_1bsb1osq _4t3i1osq _j7hql61g _5sagjx92 _1pglafbj _1o511hrf _bl8aglyw _2hwzhgkh"
};
/**
 * Derives layout size bucket from pixel dimensions.
 * Used instead of CSS @container queries (which are blocked by the linter)
 * to control which elements are shown at different card sizes.
 */
var getSize = function getSize(width, height) {
  if (width < 200 || height < 100) {
    return 'minimal';
  }
  if (width < 400 || height < 140) {
    return 'compact';
  }
  return 'full';
};
var Carousel = function Carousel(_ref) {
  var icon = _ref.icon,
    iconLabel = _ref.iconLabel,
    items = _ref.items,
    onPrimaryButtonClick = _ref.onPrimaryButtonClick,
    primaryButtonLabel = _ref.primaryButtonLabel,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'embed-card-teaser-carousel' : _ref$testId;
  var _useState = (0, _react.useState)(0),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    activeIndex = _useState2[0],
    setActiveIndex = _useState2[1];
  var _useState3 = (0, _react.useState)('full'),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    size = _useState4[0],
    setSize = _useState4[1];

  // Tracks whether the user has navigated at least once.
  // The first slide is rendered statically (no animation) to avoid any
  // CSS animation running during SSR or on the initial paint.
  var hasNavigated = (0, _react.useRef)(false);
  var containerRef = (0, _react.useRef)(null);

  // Observe the carousel container's size and update the layout bucket.
  (0, _react.useEffect)(function () {
    var el = containerRef.current;
    if (!el) {
      return;
    }
    var observer = new ResizeObserver(function (_ref2) {
      var _ref3 = (0, _slicedToArray2.default)(_ref2, 1),
        entry = _ref3[0];
      var _entry$contentRect = entry.contentRect,
        width = _entry$contentRect.width,
        height = _entry$contentRect.height;
      setSize(getSize(width, height));
    });
    observer.observe(el);
    return function () {
      return observer.disconnect();
    };
  }, []);
  var goNext = (0, _react.useCallback)(function () {
    hasNavigated.current = true;
    setActiveIndex(function (current) {
      return (current + 1) % items.length;
    });
  }, [items.length]);
  var goTo = (0, _react.useCallback)(function (index) {
    if (index === activeIndex) {
      return;
    }
    hasNavigated.current = true;
    setActiveIndex(index);
  }, [activeIndex]);
  var currentSlide = items[activeIndex];
  var isLastSlide = activeIndex === items.length - 1;
  return /*#__PURE__*/_react.default.createElement(_compiled.Box, {
    ref: containerRef,
    xcss: styles.carousel,
    testId: testId
  }, /*#__PURE__*/_react.default.createElement(_compiled.Box, {
    key: activeIndex,
    xcss: slideWrapperMap[hasNavigated.current ? 'animated' : 'static'],
    "aria-live": "polite"
  }, /*#__PURE__*/_react.default.createElement(_CarouselSlide.default, {
    primaryButtonLabel: primaryButtonLabel,
    description: currentSlide.description,
    icon: icon,
    iconLabel: iconLabel,
    image: currentSlide.image,
    title: currentSlide.title,
    onPrimaryButtonClick: onPrimaryButtonClick,
    onDotClick: goTo,
    onNextClick: !isLastSlide ? goNext : undefined,
    size: size,
    slideIndex: activeIndex,
    testId: "".concat(testId, "-slide"),
    totalSlides: items.length
  })));
};
var _default = exports.default = Carousel;