/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../WebScene.js";import n from"../../core/Error.js";import r from"../../core/Version.js";import o from"../../libs/ajv/ajv.bundle-wrapper.js";import{json as t}from"../../portal/schemas/webScene.js";import{convertAjvErrors as s}from"../../support/validationUtilsAjv.js";let i=null;function a(e,n){d(n);const r=h();return r.validate(j(n),e)?[]:s(r.errors??[])}async function c(n){let o=r.parse(n.version||"","webscene");const t=r.parse(e.version,"webscene");if(o.major===t.major&&o.minor===t.minor)return a(n);o.lessThan(1,38)&&(o=new r(1,38,"webscene"));const i=h(),c=m(o);if(null==i.getSchema(c)){const e=await fetch(l(o));if(e.ok){const n=await e.json();i.addSchema(n,c)}}return i.validate(c,n)?[]:s(i.errors??[])}async function f(e){const n=await c(e);if(n.length)throw new p(n)}function m(e){return`webscene-schema-v${e.major}.${e.minor}.json`}function l(e){return`https://js.arcgis.com/webscene-spec/${m(e)}`}function u(e){const n=e?t.definitions[j(e)]:void 0;if(!n)throw new Error(`invalid schema name to validate against '${e}'`);const r={};for(const o in n)r[o]=n[o];return r.definitions=t.definitions,r}function d(e){const n=j(e),r=h();if(!r.getSchema(n)){const o=u(e);r.addSchema(o,n)}}function h(){return i||(i=new o({allErrors:!0,extendRefs:!0}),i.addSchema(t,j()),i)}function w(){i=null}function j(e){return e?`${e}_schema.json`:"webScene_schema.json"}class p extends n{constructor(e){super("webscene:validation-failed",`WebScene validation failed with ${e.length} ${1===e.length?"error":"errors"}.`,{errors:e})}}export{p as WebSceneValidationError,f as assertValidWebScene,w as clearValidator,l as getVersionedWebSceneSchemaCDNUrl,a as validate,c as validateWebScene};
