/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{equals as r}from"../arrayUtils.js";import e from"../Error.js";import t from"../Logger.js";import{get as i}from"./get.js";import{idToReadableName as o,nameToId as n}from"./PropertyOrigin.js";import{merge as s,getProperties as u}from"./utils.js";import{originSpecificWritePropertyDefinition as l}from"./extensions/serializableProperty/utils.js";function a(r,e,t,i,o){const n={};return e.write?.writer?.call(r,i,n,t,o),n}function p(r,o,s,u,l,a){if(!u?.write)return!1;const p=i(r,s);if(!l&&u.write.overridePolicy){const e=u.write.overridePolicy.call(r,p,s,a??void 0);void 0!==e&&(l=e)}if(l||(l=u.write),!l||!1===l.enabled)return!1;if(l.layerContainerTypes&&a?.layerContainerType&&!l.layerContainerTypes.includes(a.layerContainerType))return!1;if((null===p&&!l.allowNull&&!l.writerEnsuresNonNull||void 0===p)&&l.isRequired){const i=new e("web-document-write:property-required",`Missing value for required property '${s}' on '${r.declaredClass}'`,{propertyName:s,target:r});return i&&a?.messages?a.messages.push(i):i&&!a&&t.getLogger("esri.core.accessorSupport.write").error(i.name,i.message),!1}if(void 0===p)return!1;if(null===p&&!l.allowNull&&!l.writerEnsuresNonNull)return!1;if(!l.alwaysWriteDefaults&&(!o.store.multipleOriginsSupported||0===o.store.originOf(s))&&f(r,s,a,u,p))return!1;if(!l.ignoreOrigin&&a?.origin&&o.store.multipleOriginsSupported){if(o.store.originOf(s)<n(a.origin))return!1}return!0}function f(e,t,i,o,n){const s=o.default;if(void 0===s)return!1;if(null!=o.defaultEquals)return o.defaultEquals(n);if("function"==typeof s){if(Array.isArray(n)){const o=s.call(e,t,i??void 0);return r(o,n)}return!1}return s===n}function c(r,e,t,i){const o=u(r),n=o.metadata,s=l(n[e],i);return!!s&&p(r,o,e,s,t,i)}function g(r,e,t){if(r&&"function"==typeof r.toJSON&&(!r.toJSON.isDefaultToJSON||!r.write))return s(e,r.toJSON(t));const n=u(r),f=n.metadata;for(const u in f){const c=l(f[u],t);if(!p(r,n,u,c,void 0,t))continue;const g=i(r,u),d=a(r,c,c.write&&"string"==typeof c.write.target?c.write.target:u,g,t);Object.keys(d).length>0&&(e=s(e,d),t?.resources?.pendingOperations?.length&&t.resources.pendingOperations.push(Promise.all(t.resources.pendingOperations).then(()=>s(e,d,()=>"replace-arrays"))),t?.writtenProperties&&t.writtenProperties.push({target:r,propName:u,oldOrigin:o(n.store.originOf(u)),newOrigin:t.origin}))}return e}export{c as willPropertyWrite,g as write};
