import React, { useEffect, useLayoutEffect, useRef } from 'react';
import Loading from "../Loading";
import { ModelWrap } from "./ModelWrap";
import { jsonProxy2, revokeJsonProxy } from "../..\\utils\\constants";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var Model = function Model(_ref) {
  var media = _ref.media,
    mediaVisible = _ref.mediaVisible,
    isDetail = _ref.isDetail,
    _ref$cssStyle = _ref.cssStyle,
    cssStyle = _ref$cssStyle === void 0 ? '' : _ref$cssStyle;
  var contentElement = useRef();
  var containerElement = useRef(null);
  useLayoutEffect(function () {
    var _mv$parentNode, _mv$parentNode$parent, _mv$parentNode$parent2;
    if (isDetail) {
      return;
    }
    var mv = contentElement.current;
    if (!mv) {
      return;
    }
    var shadowRoot = mv.shadowRoot;
    if (!shadowRoot) {
      return;
    }
    var button = shadowRoot.querySelectorAll('#default-poster')[0];
    button.style.background = 'none';
    var el = (_mv$parentNode = mv.parentNode) === null || _mv$parentNode === void 0 ? void 0 : (_mv$parentNode$parent = _mv$parentNode.parentNode) === null || _mv$parentNode$parent === void 0 ? void 0 : (_mv$parentNode$parent2 = _mv$parentNode$parent.parentNode) === null || _mv$parentNode$parent2 === void 0 ? void 0 : _mv$parentNode$parent2.parentNode;
    mv.style.opacity = '0';
    mv.style.width = "".concat(el === null || el === void 0 ? void 0 : el.offsetWidth, "px");
    mv.style.height = "".concat(el === null || el === void 0 ? void 0 : el.offsetHeight, "px");
    setTimeout(function () {
      mv.style.opacity = '1';
    }, mediaVisible ? 800 : 1300);
  }, [isDetail, mediaVisible]);
  useEffect(function () {
    var _contentElement$curre, _contentElement$curre2, _contentElement$curre3;
    if (!isDetail) {
      return;
    }
    var mv = contentElement.current;
    if (!mv) {
      return;
    }
    var shadowRoot = mv.shadowRoot;
    if (!shadowRoot) {
      return;
    }
    var el2 = (_contentElement$curre = contentElement.current) === null || _contentElement$curre === void 0 ? void 0 : (_contentElement$curre2 = _contentElement$curre.parentNode) === null || _contentElement$curre2 === void 0 ? void 0 : (_contentElement$curre3 = _contentElement$curre2.parentNode) === null || _contentElement$curre3 === void 0 ? void 0 : _contentElement$curre3.parentNode;
    mv.style.width = "".concat(el2 === null || el2 === void 0 ? void 0 : el2.offsetWidth, "px");
    mv.style.height = "".concat(el2 === null || el2 === void 0 ? void 0 : el2.offsetWidth, "px");
  }, []);
  return /*#__PURE__*/_jsx(ModelWrap, {
    cssStyle: cssStyle,
    children: /*#__PURE__*/_jsxs("div", {
      className: "modelContainer",
      ref: containerElement,
      children: [/*#__PURE__*/_jsx(Loading, {
        cssStyle: "\n          display: none;\n        "
      }), /*#__PURE__*/_jsx("model-viewer", {
        ref: contentElement,
        src: jsonProxy2(revokeJsonProxy(media)),
        ar: true,
        "ar-modes": "webxr scene-viewer quick-look",
        "environment-image": "neutral",
        "auto-rotate": true,
        "camera-controls": true,
        onClick: function onClick(e) {
          e.stopPropagation();
        }
      })]
    })
  });
};
export default Model;