"use strict";function parseFilterParam(e){const t={};if(!e)return t;let r={};if("string"==typeof e)try{r=JSON.parse(e)}catch(e){console_1.logger.error("USER_MEDIA","Error parsing filter param",e);return t}Object.entries(r).forEach(([e,r])=>{const i=e.split(".");let s=t;i.slice(0,-1).forEach(e=>{s[e]=s[e]||{};s=s[e]});s[i[i.length-1]]=r});return t}function buildNestedFilters(e){const t={},r={};Object.entries(e).forEach(([e,i])=>{if("boolean"==typeof i||"object"==typeof i&&"operator"in i&&"value"in i)r[e]=i;else{const r=e.split(".");let s=t;for(let e=0;e<r.length-1;e++){const t=r[e];s[t]=s[t]||{};s=s[t]}s[r[r.length-1]]=i}});return{nestedFilters:t,directFilters:r}}Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const error_1=require("@b/utils/error"),constants_1=require("@b/utils/constants"),utils_1=require("@b/api/admin/content/media/utils"),console_1=require("@b/utils/console");exports.metadata={summary:"Fetches user's own media files",operationId:"fetchUserMediaFiles",tags:["User","Media"],parameters:[...constants_1.crudParameters,{name:"uploadDir",in:"query",description:"The upload directory to filter by",schema:{type:"string"}}],responses:{200:{description:"Media entries for the user",content:{"application/json":{schema:{type:"object",properties:{items:{type:"array",items:{type:"object",additionalProperties:!0}},pagination:{type:"object",properties:{totalItems:{type:"number"},currentPage:{type:"number"},perPage:{type:"number"},totalPages:{type:"number"}}}}}}}},401:{description:"Unauthorized"},500:{description:"Internal server error"}},requiresAuth:!0};exports.default=async e=>{const{query:t,user:r,ctx:i}=e;if(!r)throw(0,error_1.createError)({statusCode:401,message:"User not authenticated"});null==i||i.step("Initializing media cache");utils_1.cacheInitialized||await(0,utils_1.initMediaWatcher)();null==i||i.step("Parsing query parameters");const s=t.page?parseInt(t.page):1,a=t.perPage?parseInt(t.perPage):50,n=t.sortField||"dateModified",o=t.sortOrder||"desc",l=t.uploadDir||"",u=["width","height"];null==i||i.step("Building filter criteria");const c=parseFilterParam(t.filter,u),{directFilters:p}=buildNestedFilters(c);null==i||i.step("Filtering user media files");const d=utils_1.mediaCache.filter(e=>{if(!/\.(jpg|jpeg|png|gif|webp)$/i.test(e.path))return!1;if(l){const t=`/uploads/${l.replace(/-/g,"/")}/`;if(!e.path.startsWith(t))return!1}return Object.entries(p).every(([t,r])=>{if(r&&"object"==typeof r&&"operator"in r){const{value:i,operator:s}=r,a=utils_1.operatorMap[s];if("function"!=typeof a)return!0;if(u.includes(t)){const r=Number(e[t]),s=parseFloat(i);return a({[t]:r},t,s)}return a(e,t,i)}return u.includes(t)?Number(e[t])===Number(r):e[t]==r})});null==i||i.step("Sorting results");d.sort((e,t)=>{const r=u.includes(n)?Number(e[n]):e[n],i=u.includes(n)?Number(t[n]):t[n];return r<i?"asc"===o?-1:1:r>i?"asc"===o?1:-1:0});null==i||i.step("Paginating results");const g=d.length,f=Math.ceil(g/a),m=(s-1)*a,h=d.slice(m,m+a);null==i||i.success(`Retrieved ${h.length} media file(s) (page ${s} of ${f})`);return{items:h,pagination:{totalItems:g,currentPage:s,perPage:a,totalPages:f}}};