/* index.tsx generated by @compiled/babel-plugin v0.38.1 */
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["ignoreContainerPadding", "onError", "placement", "testId", "overrideUrl", "style", "className"];
import "./index.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
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 { useCallback, useLayoutEffect, useState } from 'react';
import { MediaPlacement } from '../../../../../../constants';
import { Preview } from '../../../elements';
import Block from '../../block';
var previewBlockStyleCommon = null;
var ignoreContainerPaddingStyle = null;

/**
 * Represents a resolved PreviewBlock, which typically contains media or other large format content.
 * @public
 * @param {PreviewBlock} PreviewBlock
 * @see Block
 */
var PreviewBlockResolvedView = function PreviewBlockResolvedView(_ref) {
  var _ref$ignoreContainerP = _ref.ignoreContainerPadding,
    ignoreContainerPadding = _ref$ignoreContainerP === void 0 ? false : _ref$ignoreContainerP,
    onError = _ref.onError,
    placement = _ref.placement,
    testId = _ref.testId,
    overrideUrl = _ref.overrideUrl,
    style = _ref.style,
    className = _ref.className,
    blockProps = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState(style !== null && style !== void 0 ? style : {}),
    _useState2 = _slicedToArray(_useState, 2),
    dynamicStyles = _useState2[0],
    setDynamicStyles = _useState2[1];
  var updateStyles = useCallback(function () {
    if (placement === MediaPlacement.Left || placement === MediaPlacement.Right) {
      var containerPadding = ignoreContainerPadding ? '0px' : 'var(--container-padding)';
      var newStyle = _objectSpread(_objectSpread(_objectSpread({}, style), {}, {
        position: 'absolute',
        top: containerPadding,
        bottom: containerPadding,
        width: "calc(var(--preview-block-width) - ".concat(containerPadding, ")")
      }, placement === MediaPlacement.Left ? {
        left: containerPadding
      } : {}), placement === MediaPlacement.Right ? {
        right: containerPadding
      } : {});
      setDynamicStyles(newStyle);
    } else {
      if (ignoreContainerPadding) {
        setDynamicStyles(_objectSpread({}, style));
      }
    }
  }, [ignoreContainerPadding, placement, style]);

  // Should be useLayoutEffect to avoid flicker.
  // Styles should be set before paint.
  useLayoutEffect(function () {
    updateStyles();
  }, [ignoreContainerPadding, placement, updateStyles, className]);
  var handleOnLoad = useCallback(function () {
    updateStyles();
  }, [updateStyles]);
  var handleOnError = useCallback(function () {
    if (onError) {
      onError();
    }
  }, [onError]);
  return /*#__PURE__*/React.createElement(Block, _extends({}, blockProps, {
    testId: "".concat(testId, "-resolved-view"),
    // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop
    style: dynamicStyles
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop
    ,
    className: ax([placement && "_1t26n7od _ze72n7od _10aa1osq _rjuj1osq _mr051dfr", !placement && ignoreContainerPadding && "_18u01ivi _2hwxc10g _e0oi1ya9 _dmhj1ya9", className])
  }), /*#__PURE__*/React.createElement(Preview, {
    onError: handleOnError,
    onLoad: handleOnLoad,
    overrideUrl: overrideUrl
  }));
};
export default PreviewBlockResolvedView;