/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import r from"../../Error.js";import{createMD5Hash as t}from"../../MD5.js";import{isMultiOriginJSONMixin as e}from"../../multiOriginJSONSupportUtils.js";import{isAbsolute as o,isBlobProtocol as s,join as n,blobUrlToBlob as i,getPathExtension as p}from"../../urlUtils.js";import{generateUUID as c}from"../../uuid.js";import{getPropertyMetadata as u}from"../metadata.js";import{nameToId as a}from"../PropertyOrigin.js";import{propertyJSONMeta as l}from"./property.js";import{getResourceContentExtension as m}from"../../../portal/support/resourceExtension.js";import{t as d,i as f,r as y,p as g,b as h}from"../../../chunks/persistableUrlUtils.js";function j(r){const t=r?.origins??[void 0];return(e,o)=>{const s=v(r,e,o);for(const r of t){const t=l(e,r,o);for(const r in s)t[r]=s[r]}}}function v(r,t,e){if("resource"===r?.type)return U(r,t,e);switch(r?.type??"other"){case"other":return{read:!0,write:!0};case"url":{const{read:r,write:t}=h;return{read:r,write:t}}}}function U(r,t,n){const i=u(t,n);return{type:String,read:(r,t,e)=>{const o=y(r,t,e);return i.type===String?o:"function"==typeof i.type?new i.type({url:o}):void 0},write:{isRequired:i.json?.write?.isRequired,writer(t,p,c,u){if(!u?.resources)return"string"==typeof t?void(p[c]=d(t,u)):void(p[c]=t.write({},u));const l=S(t),m=d(l,{...u,verifyItemRelativeUrls:u?.verifyItemRelativeUrls?{writtenUrls:u.verifyItemRelativeUrls.writtenUrls,rootPath:void 0}:void 0},1),y=i.type!==String&&(!e(this)||u?.origin&&this.originIdOf(n)>a(u.origin)),g={object:this,propertyName:n,value:t,targetUrl:m,dest:p,targetPropertyName:c,context:u,params:r};u?.portalItem&&m&&!o(m)?y&&r?.contentAddressed?w(g):y?I(g):P(g):u?.portalItem&&(null==m||null!=f(m)||s(m)||y)?w(g):p[c]=m}}}}function w(e){const{targetUrl:o,params:p,value:u,context:a,dest:l,targetPropertyName:d}=e;if(!a.portalItem)return;const f=g(o),y=N(u,o,a);if(p?.contentAddressed&&"json"!==y.type)return void a.messages?.push(new r("persistable:contentAddressingUnsupported",`Property "${d}" is trying to serializing a resource with content of type ${y.type} with content addressing. Content addressing is only supported for json resources.`,{content:y}));const h=p?.contentAddressed&&"json"===y.type?t(y.jsonString):f?.filename??c(),j=n(p?.prefix??f?.prefix,h),v=`${j}.${m(y)}`;if(p?.contentAddressed&&a.resources&&"json"===y.type){const r=a.resources.toKeep.find(({resource:r})=>r.path===v)??a.resources.toAdd.find(({resource:r})=>r.path===v);if(r)return void(l[d]=r.resource.itemRelativeUrl)}const U=a.portalItem.resourceFromPath(v);s(o)&&a.resources&&a.resources.pendingOperations.push(i(o).then(r=>{U.path=`${j}.${m({type:"blob",blob:r})}`,l[d]=U.itemRelativeUrl}).catch(()=>{}));const w=p?.compress??!1;a.resources&&b({...e,resource:U,content:y,compress:w,updates:a.resources.toAdd}),l[d]=U.itemRelativeUrl}function I(r){const{context:t,targetUrl:e,params:o,value:s,dest:n,targetPropertyName:i}=r;if(!t.portalItem)return;const c=t.portalItem.resourceFromPath(e),u=N(s,e,t),a=m(u),l=p(c.path),d=o?.compress??!1;a===l?(t.resources&&b({...r,resource:c,content:u,compress:d,updates:t.resources.toUpdate}),n[i]=e):w(r)}function P({context:r,targetUrl:t,dest:e,targetPropertyName:o}){r.portalItem&&r.resources&&(r.resources.toKeep.push({resource:r.portalItem.resourceFromPath(t),compress:!1}),e[o]=t)}function b({object:r,propertyName:t,updates:e,resource:o,content:s,compress:n}){const i=e=>{x(r,t,e)};e.push({resource:o,content:s,compress:n,finish:i})}function N(r,t,e){return"string"==typeof r?{type:"url",url:t}:{type:"json",jsonString:JSON.stringify(r.toJSON(e))}}function S(r){return null==r?null:"string"==typeof r?r:r.url}function x(r,t,e){"string"==typeof r[t]?r[t]=e.url:r[t].url=e.url}export{j as persistable};
