import{r as a,l as B,j as h,a5 as G}from"./index-DTC0Ed21.js";import{c as J,N as Q,b as X,u as I,e as Y,g as Z,E as S,h as N,j as ee,W as te,l as ie,s as se,m as ae,a as oe}from"./TaskPanel-DL-lNNa-.js";import"./tasks-ClycqzjK.js";import"./IconUser-Bh9n--nN.js";import"./tasks-R9G1-k7v.js";import"./IconChevronLeft-DwnMQlxZ.js";import"./use-resize-observer-B2T5TxF-.js";import"./useTaskTypesData-DEDxUwkB.js";import"./PillsInput-BQBRVlfT.js";import"./SegmentedControl-CI2D3wRP.js";import"./get-base-value-JqT_q0U7.js";import"./ColorSwatch-DyLoUEeW.js";import"./TypographyStylesProvider-B4wTcLh8.js";import"./clamp-Bw9v46PI.js";import"./IconGripVertical-A4WphpSm.js";import"./Accordion-BmslGzqa.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 Ne({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),[T,E]=a.useState(!0),{isCollabActive:D}=J(),[l]=Q(),[x,b]=a.useState(!0),[n,C]=a.useState({width:0,height:0}),{fetchUploadMedia:R,fetchDeleteMedia:F,maxFileSize:de}=a.useContext(X),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);N(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?(b(!0),C({width:e,height:i})):r<s?(b(!0),C({width:"auto",height:"auto"})):(C({width:r,height:r}),b(!1))}).catch(r=>{console.warn("Ошибка при загрузке изображения:",r.message)})},[t,v]),a.useEffect(()=>{n.width&&n.height&&x&&l.update(()=>{const e=S(d);N(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);N(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);N(i)&&i.setSrc(e)})},children:h.jsx(ie,{title:W("editor.image.add_photo"),icon:"photo",onClick:()=>y(!j),isLoading:k||T})}):x?h.jsx(P,{width:n.width,height:n.height,isLoading:T,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:T,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{Ne as default};
