"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;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _extractPreview = require("../../../extractors/flexible/extract-preview");
var _blocks = require("../../FlexibleCard/components/blocks");
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); }
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) { (0, _defineProperty2.default)(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; }
var ImagePreview = function ImagePreview(_ref) {
  var fallbackElementHeight = _ref.fallbackElementHeight,
    response = _ref.response;
  var transitionStarted = (0, _react.useRef)(false);
  var previewBlockRef = (0, _react.useRef)(null);
  var _useState = (0, _react.useState)(true),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    showPreview = _useState2[0],
    setShowPreview = _useState2[1];
  var _useState3 = (0, _react.useState)({}),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    dynamicStyles = _useState4[0],
    setDynamicStyles = _useState4[1];

  // Set Preview to a fixed height to enable transitions
  var onPreviewRender = (0, _react.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 = (0, _react.useCallback)(function () {
    if (transitionStarted.current === false) {
      setDynamicStyles({
        transition: 'height 300ms ease-in-out',
        height: "".concat(fallbackElementHeight, "px")
      });
      transitionStarted.current = true;
    }
  }, [fallbackElementHeight]);
  var onPreviewTransitionEnd = (0, _react.useCallback)(function () {
    setShowPreview(false);
  }, []);
  return showPreview && (0, _extractPreview.extractSmartLinkPreviewImage)(response) ? /*#__PURE__*/_react.default.createElement(_blocks.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;
};
var _default = exports.default = ImagePreview;