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";
const Model = ({
  media,
  mediaVisible,
  isDetail,
  cssStyle: _cssStyle = ''
}) => {
  const contentElement = useRef();
  const containerElement = useRef(null);
  useLayoutEffect(() => {
    var _mv$parentNode, _mv$parentNode$parent, _mv$parentNode$parent2;
    if (isDetail) {
      return;
    }
    const mv = contentElement.current;
    if (!mv) {
      return;
    }
    const {
      shadowRoot
    } = mv;
    if (!shadowRoot) {
      return;
    }
    const button = shadowRoot.querySelectorAll('#default-poster')[0];
    button.style.background = 'none';
    const 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 = `${el === null || el === void 0 ? void 0 : el.offsetWidth}px`;
    mv.style.height = `${el === null || el === void 0 ? void 0 : el.offsetHeight}px`;
    setTimeout(() => {
      mv.style.opacity = '1';
    }, mediaVisible ? 800 : 1300);
  }, [isDetail, mediaVisible]);
  useEffect(() => {
    var _contentElement$curre, _contentElement$curre2, _contentElement$curre3;
    if (!isDetail) {
      return;
    }
    const mv = contentElement.current;
    if (!mv) {
      return;
    }
    const {
      shadowRoot
    } = mv;
    if (!shadowRoot) {
      return;
    }
    const 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 = `${el2 === null || el2 === void 0 ? void 0 : el2.offsetWidth}px`;
    mv.style.height = `${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: `
          display: none;
        `
      }), /*#__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: e => {
          e.stopPropagation();
        }
      })]
    })
  });
};
export default Model;