import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React, { useCallback, useRef, useState } from 'react';
import { extractSmartLinkPreviewImage } from '../../../extractors/flexible/extract-preview';
import { PreviewBlock } from '../../FlexibleCard/components/blocks';
var ImagePreview = function ImagePreview(_ref) {
  var fallbackElementHeight = _ref.fallbackElementHeight,
    response = _ref.response;
  var transitionStarted = useRef(false);
  var previewBlockRef = useRef(null);
  var _useState = useState(true),
    _useState2 = _slicedToArray(_useState, 2),
    showPreview = _useState2[0],
    setShowPreview = _useState2[1];
  var _useState3 = useState({}),
    _useState4 = _slicedToArray(_useState3, 2),
    dynamicStyles = _useState4[0],
    setDynamicStyles = _useState4[1];

  // Set Preview to a fixed height to enable transitions
  var onPreviewRender = useCallback(function () {
    if (previewBlockRef.current) {
      var _previewBlockRef$curr;
      var previewHeight = (_previewBlockRef$curr = previewBlockRef.current) === null || _previewBlockRef$curr === void 0 ? void 0 : _previewBlockRef$curr.getBoundingClientRect().height;
      setDynamicStyles(_objectSpread({
        borderTopLeftRadius: "var(--ds-radius-large, 8px)",
        borderTopRightRadius: "var(--ds-radius-large, 8px)"
      }, previewHeight ? {
        height: "".concat(previewHeight, "px")
      } : {}));
    }
  }, []);

  // On error set Preview to Fallback height with transition
  var onPreviewError = useCallback(function () {
    if (transitionStarted.current === false) {
      setDynamicStyles({
        transition: 'height 300ms ease-in-out',
        height: "".concat(fallbackElementHeight, "px")
      });
      transitionStarted.current = true;
    }
  }, [fallbackElementHeight]);
  var onPreviewTransitionEnd = useCallback(function () {
    setShowPreview(false);
  }, []);
  return showPreview && extractSmartLinkPreviewImage(response) ? /*#__PURE__*/React.createElement(PreviewBlock, {
    onError: onPreviewError,
    ignoreContainerPadding: true,
    onTransitionEnd: onPreviewTransitionEnd,
    blockRef: previewBlockRef,
    onRender: onPreviewRender
    // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop
    ,
    style: dynamicStyles
  }) : null;
};
export default ImagePreview;