import{r as a,b as B,j as h,X as G}from"./index-n-59FgrC.js";import{c as X,N as J,b as Q,u as I,e as Y,g as Z,E as S,h as b,j as ee,W as te,l as ie,s as se,m as ae,a as oe}from"./TaskPanel-FSBo9x0f.js";import"./useTaskTypesData-CgG6u01U.js";import"./IconUser-BvK8eLe8.js";import"./tasks-CZQGFm5u.js";import"./get-env-uyVen0u2.js";import"./IconChevronLeft-CyT72r93.js";import"./use-resize-observer-Bh8oaK16.js";import"./SegmentedControl-D05EYfFd.js";import"./SimpleGrid-DzebcIbT.js";import"./get-base-value-JqT_q0U7.js";import"./TypographyStylesProvider-D2Jq4B6Y.js";import"./clamp--03mf-JJ.js";import"./IconSortDescending-DCVZ-bvF.js";import"./IconGripVertical-Ce_4nROx.js";import"./Accordion-DVbX6gPb.js";const P=t=>{const{width:g,height:d,isLoading:o,isError:c,src:m,id:p,altText:f,imageRef:w,maxWidth:u}=t;return h.jsxs("div",{className:"media-wrapper",style:{width:g,height:d,display:"inline-flex",position:"relative"},children:[h.jsx(G,{visible:o,zIndex:1e3,overlayProps:{backgroundOpacity:.5},loaderProps:{size:24,color:"black"}}),h.jsx(re,{className:null,src:m,altText:f,imageRef:w,width:"inherit",height:"inherit",maxWidth:u||"auto",id:p})]})};function re({altText:t,className:g,imageRef:d,src:o,width:c,height:m,maxWidth:p,id:f}){const[w,u]=a.useState(!1);return h.jsx("img",{className:`${g||void 0} ${w?"":"loading"}`,src:o,alt:t,ref:d,style:{height:m,maxWidth:p,width:c},onLoad:()=>{o.includes("splash")?setTimeout(()=>{u(!0)},100):u(!0)},draggable:"false","data-id":f})}function be({src:t,altText:g,nodeKey:d,width:o,height:c,maxWidth:m,showCaption:p,caption:f,id:w,create:u,alignment:V,isLoading:k}){var L,z;const A=a.useRef(null),[N,E]=a.useState(!0),{isCollabActive:D}=X(),[l]=J(),[x,T]=a.useState(!0),[n,C]=a.useState({width:0,height:0}),{fetchUploadMedia:R,fetchDeleteMedia:F,maxFileSize:de}=a.useContext(Q),v=I.has(t)&&(((L=I.get(t))==null?void 0:L.mediaNodeKeys.length)===0||((z=I.get(t))==null?void 0:z.mediaNodeKeys.length)===void 0||t.trim()===""),{t:W}=B(),[ne,O]=a.useState(0);a.useEffect(()=>l.registerDecoratorListener(()=>{O(e=>e+1)}),[v]),a.useEffect(()=>{if(!t||t.startsWith("data:image/")||!t.trim()){E(!1);return}const e=new Image;e.src=t,e.onload=()=>E(!1),e.onerror=()=>{E(!1)}},[t,v]);const U=(e,i)=>{l.update(()=>{oe(null);const s=S(d);b(s)&&s.setWidthAndHeight(e,i)})},{historyState:K}=Y(),{settings:{showNestedEditorTreeView:M}}=Z(),[j,y]=a.useState(!1);function $(e){return e.endsWith(".svg")||e.includes("image/svg+xml")}function q(e){return new Promise((i,s)=>{if($(e)){i(0);return}const r=new Image;r.src=e,r.onload=()=>{i(r.width)},r.onerror=()=>{s(new Error("Failed to load image"))}})}a.useEffect(()=>{u&&y(!0)},[u]),a.useEffect(()=>{let e=o,i=c,s=100;(t.includes("unsplash")?t.trim():!v&&t.trim()||t.includes("upload-src-editor"))&&t!=="upload"&&q(t).then(r=>{typeof e=="number"&&e<s&&r<s?(T(!0),C({width:e,height:i})):r<s?(T(!0),C({width:"auto",height:"auto"})):(C({width:r,height:r}),T(!1))}).catch(r=>{console.warn("Ошибка при загрузке изображения:",r.message)})},[t,v]),a.useEffect(()=>{n.width&&n.height&&x&&l.update(()=>{const e=S(d);b(e)&&e.setWidthAndHeight(n.width,n.height)})},[n.width,n.height,x]),a.useEffect(()=>{const e=document.documentElement;ee(j,e)},[j]);const H=(e,i)=>{E(!1),l.update(()=>{const s=S(d);b(s)&&(s.setSrc(e),s.setId(i))})},_=e=>{E(!0),R&&R(e[0],(i,s)=>{H(i,s)},i=>{i&&ae({message:W("editor.gallery.errorFile")}),H("","")})};return!t||t==="upload"?h.jsx(te,{uploading:!1,onUpload:e=>{!R||!e.length||(_(e),y(!1))},popoverProps:{opened:j,position:"bottom",onChange:y},onChange:e=>{y(!1),l.update(()=>{const i=S(d);b(i)&&i.setSrc(e)})},children:h.jsx(ie,{title:W("editor.image.add_photo"),icon:"photo",onClick:()=>y(!j),isLoading:k||N})}):x?h.jsx(P,{width:n.width,height:n.height,isLoading:N,isError:!1,isIcon:x,src:t,id:w,editor:l,nodeKey:d,fetchDeleteMedia:F,altText:g,imageRef:A,maxWidth:m,showCaption:p,caption:f,isCollabActive:D,historyState:K,showNestedEditorTreeView:M,t:W}):h.jsx(se,{editor:l,isIcon:x,width:typeof o=="string"&&o.includes("%")?parseFloat(o):o,height:typeof c=="string"&&c.includes("%")?parseFloat(c):o,initialWidth:n.width||"",initialHeight:n.height||"",onResizeEnd:U,alignment:V,children:h.jsx(P,{width:"inherit",height:"inherit",isLoading:N,isError:!1,isIcon:x,src:t,id:w,editor:l,nodeKey:d,fetchDeleteMedia:F,altText:g,imageRef:A,maxWidth:m,showCaption:p,caption:f,isCollabActive:D,historyState:K,showNestedEditorTreeView:M,t:W})})}export{be as default};
