/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../arrayUtils","../Error","../Logger","./PropertyOrigin","./utils","./extensions/serializableProperty"],(function(e,r,i,t,n,o,s){"use strict";const l=t.getLogger("esri.core.accessorSupport.write");function u(e,r,i,t,n){var o,s;const l={};return null==(o=r.write)||null==(s=o.writer)||s.call(e,t,l,i,n),l}function a(e,r,t,o,s,u){if(!o||!o.write)return!1;const a=e.get(t);if(!s&&o.write.overridePolicy){const r=o.write.overridePolicy.call(e,a,t,u);void 0!==r&&(s=r)}if(s||(s=o.write),!s||!1===s.enabled)return!1;if((null===a&&!s.allowNull&&!s.writerEnsuresNonNull||void 0===a)&&s.isRequired){const r=new i("web-document-write:property-required",`Missing value for required property '${t}' on '${e.declaredClass}'`,{propertyName:t,target:e});return r&&u&&u.messages?u.messages.push(r):r&&!u&&l.error(r.name,r.message),!1}if(void 0===a)return!1;if(null===a&&!s.allowNull&&!s.writerEnsuresNonNull)return!1;if((!r.store.multipleOriginsSupported||r.store.originOf(t)===n.OriginId.DEFAULTS)&&g(e,t,u,o,a))return!1;if(!s.ignoreOrigin&&u&&u.origin&&r.store.multipleOriginsSupported){if(r.store.originOf(t)<n.nameToId(u.origin))return!1}return!0}function g(e,i,t,n,o){const s=n.default;if(void 0===s)return!1;if(null!=n.defaultEquals)return n.defaultEquals(o);if("function"==typeof s){if(Array.isArray(o)){const n=s.call(e,i,t);return r.equals(n,o)}return!1}return s===o}function f(e,r,i,t){const n=o.getProperties(e),l=n.metadatas,u=s.originSpecificWritePropertyDefinition(l[r],t);return!!u&&a(e,n,r,u,i,t)}function c(e,r,i){if(e&&"function"==typeof e.toJSON&&(!e.toJSON.isDefaultToJSON||!e.write))return o.merge(r,e.toJSON(i));const t=o.getProperties(e),l=t.metadatas;for(const c in l){const p=s.originSpecificWritePropertyDefinition(l[c],i);if(!a(e,t,c,p,void 0,i))continue;const d=e.get(c),w=u(e,p,p.write&&"string"==typeof p.write.target?p.write.target:c,d,i);var g,f;if(Object.keys(w).length>0)r=o.merge(r,w),null!=i&&null!=(g=i.resources)&&null!=(f=g.pendingOperations)&&f.length&&Promise.all(i.resources.pendingOperations).then((()=>o.merge(r,w))),i&&i.writtenProperties&&i.writtenProperties.push({target:e,propName:c,oldOrigin:n.idToReadableName(t.store.originOf(c)),newOrigin:i.origin})}return r}e.willPropertyWrite=f,e.write=c,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
