/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../Error","../../../Logger","../../../object","./type"],(function(e,r,t,n,i){"use strict";const o=t.getLogger("esri.core.accessorSupport.extensions.serializableProperty.writer");function u(e,r){var t;if(!r.write||r.write.writer||!1===r.write.enabled&&!r.write.overridePolicy)return;const n=null!=(t=null==e?void 0:e.ndimArray)?t:0;e&&(1===n||"type"in e&&i.isCollection(e.type))?r.write.writer=y:n>1?r.write.writer=A(n):r.types?Array.isArray(r.types)?r.write.writer=a(r.types[0]):r.write.writer=s(r.types):r.write.writer=f}function s(e){return(r,t,n,i)=>r?l(r,e,i)?f(r,t,n,i):void 0:f(r,t,n,i)}function l(e,t,n){for(const r in t.typeMap)if(e instanceof t.typeMap[r])return!0;if(null!=n&&n.messages){var i,u;const s=null!=(i=t.errorContext)?i:"type",l=`Values of type '${null!=(u="function"!=typeof t.key?e[t.key]:e.declaredClass)?u:"Unknown"}' cannot be written`;n&&n.messages&&e&&n.messages.push(new r(`${s}:unsupported`,l,{definition:e,context:n})),o.error(l)}return!1}function a(e){return(r,t,n,i)=>{if(!r||!Array.isArray(r))return f(r,t,n,i);return f(r.filter((r=>l(r,e,i))),t,n,i)}}function f(e,r,t,i){n.setDeepValue(t,c(e,i),r)}function c(e,r){return e&&"function"==typeof e.write?e.write({},r):e&&"function"==typeof e.toJSON?e.toJSON():"number"==typeof e?p(e):e}function p(e){return e===-1/0?-Number.MAX_VALUE:e===1/0?Number.MAX_VALUE:isNaN(e)?null:e}function y(e,r,t,i){let o;null===e?o=null:e&&"function"==typeof e.map?(o=e.map((e=>c(e,i))),"function"==typeof o.toArray&&(o=o.toArray())):o=[c(e,i)],n.setDeepValue(t,o,r)}function w(e,r,t){return 0!==t&&Array.isArray(e)?e.map((e=>w(e,r,t-1))):c(e,r)}function A(e){return(r,t,i,o)=>{let u;if(null===r)u=null;else{u=w(r,o,e);let t=e,n=u;for(;t>0&&Array.isArray(n);)t--,n=n[0];if(void 0!==n)for(let e=0;e<t;e++)u=[u]}n.setDeepValue(i,u,t)}}e.create=u,e.numberToJSON=p,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
