/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../Logger","../../../object","../../../Warning","../../metadata","./type"],(function(e,t,r,n,o,i){"use strict";const u=t.getLogger("esri.core.accessorSupport.extensions.serializableProperty.reader");function p(e,t,n){var o,i;e&&(!n&&!t.read||null!=(o=t.read)&&o.reader||!1===(null==(i=t.read)?void 0:i.enabled)||v(e)&&r.setDeepValue("read.reader",s(e),t))}function s(e){var t;const r=null!=(t=e.ndimArray)?t:0;if(r>1)return f(e);if(1===r)return l(e);if("type"in e&&d(e.type)){var n,o;const t=null==(n=e.type.prototype)||null==(o=n.itemType)?void 0:o.Type,r=l("function"==typeof t?{type:t}:{types:t});return(t,n,o)=>{const i=r(t,n,o);return i?new e.type(i):i}}return y(e)}function y(e){return"type"in e?a(e.type):j(e.types)}function a(e){return e.prototype.read?(t,r,n)=>{if(null==t)return t;const o=typeof t;if("object"!==o)return void u.error(`Expected JSON value of type 'object' to deserialize type '${e.prototype.declaredClass}', but got '${o}'`);const i=new e;return i.read(t,n),i}:e.fromJSON}function c(e,t,r,n){return 0!==n&&Array.isArray(t)?t.map((t=>c(e,t,r,n-1))):e(t,void 0,r)}function f(e){var t;const r=y(e),n=c.bind(null,r),o=null!=(t=e.ndimArray)?t:0;return(e,t,r)=>{if(null==e)return e;e=n(e,r,o);let i=o,u=e;for(;i>0&&Array.isArray(u);)i--,u=u[0];if(void 0!==u)for(let n=0;n<i;n++)e=[e];return e}}function l(e){const t=y(e);return(e,r,n)=>{if(null==e)return e;if(Array.isArray(e)){const r=[];for(const o of e){const e=t(o,void 0,n);void 0!==e&&r.push(e)}return r}const o=t(e,void 0,n);return void 0!==o?[o]:void 0}}function d(e){if(!i.isCollection(e))return!1;const t=e.prototype.itemType;return!(!t||!t.Type)&&("function"==typeof t.Type?g(t.Type):b(t.Type))}function v(e){return"types"in e?b(e.types):g(e.type)}function g(e){return!Array.isArray(e)&&(!!e&&e.prototype&&("read"in e.prototype||"fromJSON"in e||d(e)))}function b(e){for(const t in e.typeMap){if(!g(e.typeMap[t]))return!1}return!0}function j(e){var t;let r=null;const o=null!=(t=e.errorContext)?t:"type";return(t,i,p)=>{if(null==t)return t;const s=typeof t;if("object"!==s)return void u.error(`Expected JSON value of type 'object' to deserialize, but got '${s}'`);r||(r=A(e));const y=e.key;if("string"!=typeof y)return;const a=t[y],c=a?r[a]:e.defaultKeyValue?e.typeMap[e.defaultKeyValue]:void 0;if(!c){const e=`Type '${a||"unknown"}' is not supported`;return p&&p.messages&&t&&p.messages.push(new n(`${o}:unsupported`,e,{definition:t,context:p})),void u.error(e)}const f=new c;return f.read(t,p),f}}function A(e){const t={};for(const i in e.typeMap){var r,n;const u=e.typeMap[i],p=o.getOwnClassMetadata(u.prototype);if("function"==typeof e.key)continue;const s=p.properties[e.key];if(!s)continue;null!=(r=s.json)&&r.type&&Array.isArray(s.json.type)&&1===s.json.type.length&&"string"==typeof s.json.type[0]&&(t[s.json.type[0]]=u);const y=null==(n=s.json)?void 0:n.write;if(!y||!y.writer){t[i]=u;continue}const a=y.target,c="string"==typeof a?a:e.key,f={};y.writer(i,f,c),f[c]&&(t[f[c]]=u)}return t}e.create=p,e.createTypeReader=s,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
