/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import r from"../../../Logger.js";import{setDeepValue as t}from"../../../object.js";import{isCollection as e}from"./type.js";import{addUnsupportedMessage as n}from"../../../../webdoc/support/unsupportedErrors.js";function o(r,t){if(!t.write||t.write.writer||!1===t.write.enabled&&!t.write.overridePolicy)return;const n=r?.ndimArray??0;r&&(1===n||"type"in r&&e(r.type))?t.write.writer=a:n>1?t.write.writer=l(n):t.types?Array.isArray(t.types)?t.write.writer=f(t.types[0]):t.write.writer=i(t.types):t.write.writer=s}function i(r){return(t,e,n,o)=>t?u(t,r,o)?s(t,e,n,o):void 0:s(t,e,n,o)}function u(t,e,o){for(const r in e.typeMap)if(t instanceof e.typeMap[r])return!0;if(o?.messages){const i=e.errorContext??"type",u=`Values of type '${("function"!=typeof e.key?t[e.key]:t.declaredClass)??"Unknown"}' cannot be written`;o&&o.messages&&t&&n(o,"error",i,u,{definition:t,context:o}),r.getLogger("esri.core.accessorSupport.extensions.serializableProperty.writer").error(u)}return!1}function f(r){return(t,e,n,o)=>{if(!t||!Array.isArray(t))return s(t,e,n,o);return s(t.filter(t=>u(t,r,o)),e,n,o)}}function s(r,e,n,o){t(n,p(r,o),e)}function p(r,t){return r&&"function"==typeof r.write?r.write({},t):r&&"function"==typeof r.toJSON?r.toJSON():"number"==typeof r?y(r):r}function y(r){return r===-1/0?-Number.MAX_VALUE:r===1/0?Number.MAX_VALUE:isNaN(r)?null:r}function a(r,e,n,o){let i;null===r?i=null:r&&"function"==typeof r.map?(i=r.map(r=>p(r,o)),"function"==typeof i.toArray&&(i=i.toArray())):i=[p(r,o)],t(n,i,e)}function c(r,t,e){return 0!==e&&Array.isArray(r)?r.map(r=>c(r,t,e-1)):p(r,t)}function l(r){return(e,n,o,i)=>{let u;if(null===e)u=null;else{u=c(e,i,r);let t=r,n=u;for(;t>0&&Array.isArray(n);)t--,n=n[0];if(void 0!==n)for(let r=0;r<t;r++)u=[u]}t(o,u,n)}}export{o as create,y as numberToJSON};
