/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "./index.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { Box } from '@atlaskit/primitives/compiled';
import CarouselSlide from './CarouselSlide';
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 = useState(0),
    _useState2 = _slicedToArray(_useState, 2),
    activeIndex = _useState2[0],
    setActiveIndex = _useState2[1];
  var _useState3 = useState('full'),
    _useState4 = _slicedToArray(_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 = useRef(false);
  var containerRef = useRef(null);

  // Observe the carousel container's size and update the layout bucket.
  useEffect(function () {
    var el = containerRef.current;
    if (!el) {
      return;
    }
    var observer = new ResizeObserver(function (_ref2) {
      var _ref3 = _slicedToArray(_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 = useCallback(function () {
    hasNavigated.current = true;
    setActiveIndex(function (current) {
      return (current + 1) % items.length;
    });
  }, [items.length]);
  var goTo = 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.createElement(Box, {
    ref: containerRef,
    xcss: styles.carousel,
    testId: testId
  }, /*#__PURE__*/React.createElement(Box, {
    key: activeIndex,
    xcss: slideWrapperMap[hasNavigated.current ? 'animated' : 'static'],
    "aria-live": "polite"
  }, /*#__PURE__*/React.createElement(CarouselSlide, {
    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
  })));
};
export default Carousel;