"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("react/jsx-runtime"),n=require("react"),_=require("react-i18next"),i=require("./layers.CDXJ2Sx6.js"),B=require("@mantine/core"),G=require("@layers-app/shared"),k=t=>{const{width:f,height:d,isLoading:a,isError:h,src:m,id:p,altText:I,imageRef:x,maxWidth:c}=t;return g.jsxs("div",{className:"media-wrapper",style:{width:f,height:d,display:"inline-flex",position:"relative"},children:[g.jsx(B.LoadingOverlay,{visible:a,zIndex:1e3,overlayProps:{backgroundOpacity:.5},loaderProps:{size:24,color:"black"}}),g.jsx(V,{className:null,src:m,altText:I,imageRef:x,width:"inherit",height:"inherit",maxWidth:c||"auto",id:p})]})};function V({altText:t,className:f,imageRef:d,src:a,width:h,height:m,maxWidth:p,id:I}){const[x,c]=n.useState(!1);return g.jsx("img",{className:`${f||void 0} ${x?"":"loading"}`,src:a,alt:t,ref:d,style:{height:m,maxWidth:p,width:h},onLoad:()=>{a.includes("splash")?setTimeout(()=>{c(!0)},100):c(!0)},draggable:"false","data-id":I})}function J({src:t,altText:f,nodeKey:d,width:a,height:h,maxWidth:m,showCaption:p,caption:I,id:x,create:c,alignment:F}){var A,$;const M=n.useRef(null),[v,S]=n.useState(!0),{isCollabActive:W}=i.l(),[u]=i.o(),[E,j]=n.useState(!0),[l,C]=n.useState({width:0,height:0}),{fetchUploadMedia:L,fetchDeleteMedia:P,maxFileSize:Q}=n.useContext(i.Context),N=i.mediaMap.has(t)&&(((A=i.mediaMap.get(t))==null?void 0:A.mediaNodeKeys.length)===0||(($=i.mediaMap.get(t))==null?void 0:$.mediaNodeKeys.length)===void 0||t.trim()===""),{t:y}=_.useTranslation(),[X,H]=n.useState(0);n.useEffect(()=>u.registerDecoratorListener(()=>{H(e=>e+1)}),[N]),n.useEffect(()=>{if(!t||t.startsWith("data:image/")||!t.trim()){S(!1);return}const e=new Image;e.src=t,e.onload=()=>S(!1),e.onerror=()=>{S(!1)}},[t,N]);const U=(e,o)=>{u.update(()=>{i.ms(null);const s=i.us(d);i.$isImageNode(s)&&s.setWidthAndHeight(e,o)})},{historyState:R}=i.useSharedHistoryContext(),{settings:{showNestedEditorTreeView:z}}=i.useSettings(),[b,w]=n.useState(!1);function D(e){return e.endsWith(".svg")||e.includes("image/svg+xml")}function T(e){return new Promise((o,s)=>{if(D(e)){o(0);return}const r=new Image;r.src=e,r.onload=()=>{o(r.width)},r.onerror=()=>{s(new Error("Failed to load image"))}})}n.useEffect(()=>{c&&w(!0)},[c]),n.useEffect(()=>{let e=a,o=h,s=100;(t.includes("unsplash")?t.trim():!N&&t.trim()||t.includes("upload-src-editor"))&&t!=="upload"&&T(t).then(r=>{typeof e=="number"&&e<s&&r<s?(j(!0),C({width:e,height:o})):r<s?(j(!0),C({width:"auto",height:"auto"})):(C({width:r,height:r}),j(!1))}).catch(r=>{console.warn("Ошибка при загрузке изображения:",r.message)})},[t,N]),n.useEffect(()=>{l.width&&l.height&&E&&u.update(()=>{const e=i.us(d);i.$isImageNode(e)&&e.setWidthAndHeight(l.width,l.height)})},[l.width,l.height,E]),n.useEffect(()=>{const e=document.documentElement;i.toggleLockScroll(b,e)},[b]),n.useEffect(()=>u.registerCommand(i.UPLOAD_IMAGE_COMMAND,e=>(t==="upload"&&q(e),!1),i.Ii),[t,v]);const O=(e,o)=>{S(!1),u.update(()=>{const s=i.us(d);i.$isImageNode(s)&&(s.setSrc(e),s.setId(o))})},q=e=>{S(!0),L&&L(e[0],(o,s)=>{O(o,s)},o=>{o&&G.showErrorNotification({message:y("editor.gallery.errorFile")}),O("","")})};return!t||t==="upload"?g.jsx(i.ImagePicker,{uploading:!1,onUpload:e=>{!L||!e.length||(q(e),w(!1))},popoverProps:{opened:b,position:"bottom",onChange:w},onChange:e=>{w(!1),u.update(()=>{const o=i.us(d);i.$isImageNode(o)&&o.setSrc(e)})},children:g.jsx(i.EmptyNodeButton,{title:y("editor.image.add_photo"),icon:"photo",onClick:()=>w(!b),isLoading:v})}):E?g.jsx(k,{width:l.width,height:l.height,isLoading:v,isError:!1,isIcon:E,src:t,id:x,editor:u,nodeKey:d,fetchDeleteMedia:P,altText:f,imageRef:M,maxWidth:m,showCaption:p,caption:I,isCollabActive:W,historyState:R,showNestedEditorTreeView:z,t:y}):g.jsx(i.ResizableContainer,{editor:u,isIcon:E,width:typeof a=="string"&&a.includes("%")?parseFloat(a):a,height:typeof h=="string"&&h.includes("%")?parseFloat(h):a,initialWidth:l.width||"",initialHeight:l.height||"",onResizeEnd:U,alignment:F,children:g.jsx(k,{width:"inherit",height:"inherit",isLoading:v,isError:!1,isIcon:E,src:t,id:x,editor:u,nodeKey:d,fetchDeleteMedia:P,altText:f,imageRef:M,maxWidth:m,showCaption:p,caption:I,isCollabActive:W,historyState:R,showNestedEditorTreeView:z,t:y})})}exports.default=J;
