import{r as e}from"./chunk-QTnfLwEv.js";import{m as t,n}from"./index-7yDgay5u.js";import{i as r,o as i,t as a}from"./jsx-runtime-DHSfUA5-.js";import{P as o,f as s}from"./test.id-DxQrYxPk.js";import{n as c}from"./fileAssetService-gA4Q8V3n.js";import{t as l}from"./PageHeader-CzIJsNRF.js";import{t as u}from"./play-Bqy5oG2o.js";import{t as d}from"./useAttachmentThumbUrl-D6z1SOlU.js";import{f,o as p}from"./route-CPr17rXr.js";import{t as m}from"./useSuccessToast-CW8oLO3e.js";import{a as h,m as g,s as _,t as v,v as y,w as b}from"./selectors-DZWoTA9D.js";import{t as x}from"./useDiaryModel-BQGj9xs8.js";import{n as S,t as C}from"./useVideoPlayer-wOvz1MmI.js";function w(e,t,n=Date.now()){if(!g(e,t))return[];let r=new Map;for(let i of e.entries){if(i.deletedAt||y(i,n)||i.notebookId!==t||i.type!==`attachment`||!i.attachmentId)continue;let a=h(e,i.attachmentId),o=a?.type===`image`?a:void 0,s=a?.type===`video`?a:void 0;if(!o&&!s)continue;let c=b(new Date(_(i)),`yyyy-MM`),l=r.get(c)??[];l.push({entry:i,image:o,video:s}),r.set(c,l)}let i=[];for(let[e,t]of r)t.sort((e,t)=>v(t.entry,e.entry)),i.push({monthKey:e,monthStart:new Date(`${e}-01T00:00:00`).getTime(),items:t});return i.sort((e,t)=>t.monthKey.localeCompare(e.monthKey)),i}function T(e){return e.flatMap(e=>e.items.flatMap(e=>e.image?[e.image]:[]))}var E=e(i(),1),D=a();function O({attachment:e,previewImages:t}){let n=d(e,{role:`thumbnail`}),i=S(),a=(0,E.useRef)(null);return(0,D.jsx)(`button`,{ref:a,type:`button`,className:r.DiaryMediaPage.Cell,"data-test-id":s.imageButton,onClick:()=>i({attachments:t.length?t:[e],initialAttachmentId:e.id,originRef:a}),children:n?(0,D.jsx)(`img`,{className:r.DiaryMediaPage.Image,"data-test-id":s.image,src:n,alt:``}):(0,D.jsx)(`span`,{className:r.DiaryMediaPage.Placeholder})})}function k({attachment:e}){let t=o(c),i=m(),a=C(),l=d(e,{role:`thumbnail`});return(0,D.jsxs)(`button`,{type:`button`,className:r.DiaryMediaPage.Cell,"data-test-id":s.videoButton,onClick:()=>{let r=e.s3Key;if(!r){i({message:n(`diary.video.missing`,`Video is missing`),icon:`none`});return}a({loadUrl:()=>t.getFileUrl(r,{role:`large`})})},children:[l?(0,D.jsx)(`img`,{className:r.DiaryMediaPage.Image,"data-test-id":s.videoThumbnail,src:l,alt:``}):(0,D.jsx)(`span`,{className:r.DiaryMediaPage.Placeholder,"data-test-id":s.videoThumbnail,"aria-hidden":`true`}),(0,D.jsx)(`span`,{className:r.DiaryMediaPage.VideoPlayBadge,"aria-hidden":`true`,children:(0,D.jsx)(u,{size:15,fill:`#ffffff`,color:`#ffffff`,className:r.DiaryMediaPage.VideoPlayIcon})})]})}function A({groups:e,previewImages:i}){let a=t();return(0,D.jsx)(D.Fragment,{children:e.map(e=>(0,D.jsxs)(`section`,{className:r.DiaryMediaPage.Section,children:[(0,D.jsx)(`h2`,{className:r.DiaryMediaPage.MonthTitle,"data-test-id":s.month,children:b(e.monthStart,n(`calendar.monthTitleFormat`,`MMMM yyyy`),{locale:a})}),(0,D.jsx)(`div`,{className:r.DiaryMediaPage.Grid,children:e.items.map(e=>e.image?(0,D.jsx)(O,{attachment:e.image,previewImages:i},e.entry.id):e.video?(0,D.jsx)(k,{attachment:e.video},e.entry.id):null)})]},e.monthKey))})}function j(){let{notebookId:e}=f(),t=x(),i=e?g(t,e):void 0,a=(0,E.useMemo)(()=>e?w(t,e):[],[t,e]),o=(0,E.useMemo)(()=>T(a),[a]);return!i||!e?(0,D.jsx)(p,{to:`/diaries`,replace:!0}):(0,D.jsxs)(`div`,{className:r.Page.Root,"data-test-id":s.page,children:[(0,D.jsx)(l,{title:n(`diary.media`,`Images and videos`),showBack:!0}),(0,D.jsx)(`main`,{className:r.DiaryMediaPage.Content,"data-test-id":s.content,children:a.length===0?(0,D.jsx)(`div`,{className:r.DiaryMediaPage.Empty,"data-test-id":s.empty,children:n(`diary.media.empty`,`No images or videos yet`)}):(0,D.jsx)(A,{groups:a,previewImages:o})})]})}export{j as DiaryChatMediaPage};
//# sourceMappingURL=media-DR_gBUGW.js.map