/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","./aaBoundingRect","./boundsUtils","./jsonUtils","./spatialReferenceUtils"],(function(e,t,s,n,o){"use strict";function f(e){if(!e)return null;if(!e.spatialReference)return e;const t=o.getInfo(e.spatialReference);if(!t)return e;const s=t.valid[1];return n.isPolygon(e)?r(e,s):n.isPolyline(e)?i(e,s):e}function i(e,n){let o={paths:[],spatialReference:e.spatialReference};const[f,,i]=s.getBoundsXY(t.create(),e);let r=Math.ceil((f-n)/(2*n))*(2*n)+n;if(r>i)o=e;else{let t,s;for(;r<i;){const f=u(e,r);if(t=f[0],s=f[1],t)for(const e of t.paths)o.paths.push(e);if(null==s)break;e=s,r+=2*n}if(s)for(const e of s.paths)o.paths.push(e)}for(const t of o.paths){const e=g(s.getPointsBoundsCenterX(t),n);for(const s of t)s[0]-=2*e*n}return o}function r(e,n){let o={rings:[],spatialReference:e.spatialReference};const[f,,i]=s.getBoundsXY(t.create(),e);let r=Math.ceil((f-n)/(2*n))*(2*n)+n;if(r>i)o=e;else{let t,s;for(;r<i;){if([t,s]=h(e,r),t)for(const e of t.rings)o.rings.push(e);if(null==s)break;e=s,r+=2*n}if(s)for(const e of s.rings)o.rings.push(e)}for(const t of o.rings){const e=g(s.getPointsBoundsCenterX(t),n);for(const s of t)s[0]-=2*e*n}return o}const l=t.create();function u(e,t){const[n,,o]=s.getBoundsXY(l,e);let f,i;if(o<=t)f=e;else if(n>=t)i=e;else{f={paths:[]},i={paths:[]};const n=f.paths,o=i.paths;for(const f of e.paths){const[e,,i]=s.getPointsBounds(l,f);if(i<=t)n.push(f);else if(e>=t)o.push(f);else{let e=f[0],s=p(e,t),i=[];i.push(e);for(let r=1;r<f.length;r++){const l=f[r],u=p(l,t);if(u===s)i.push(l);else{const f=a(e,l,t);i.push(f),s?n.push(i):o.push(i),i=[f,l],s=u}e=l}s?n.push(i):o.push(i)}}}return[f,i]}function h(e,n){let o,f;const[i,,r]=s.getBoundsXY(t.create(),e);if(r<=n)o=e;else if(i>=n)f=e;else{const i=[],r=[],l=[];for(const o of e.rings){const[e,,f]=s.getPointsBounds(t.create(),o);if(f<=n)i.push(o);else if(e>=n)r.push(o);else{let e=o[o.length-1],t=p(e,n),s=[];for(const f of o){const o=p(f,n);if(o===t)s.push(f);else{const u=a(e,f,n);s.push(u),t?i.push(s):r.push(s),l.push(u[1]),s=[],s.push([u[0],u[1]]),s.push(f),t=o}e=f}t?i.push(s):r.push(s)}}l.sort(((e,t)=>e-t));for(let e=0;e<l.length-1;e+=2){const t=[];t.push([n,l[e]]),t.push([n,l[e+1]]),i.length>0&&i.push(t),r.length>0&&r.push(t)}i.length>0&&(o=c(i)),r.length>0&&(f=c(r))}return[o,f]}function c(e){let t;const n={rings:[]};for(;e.length>0;){const o=e[0];let f=!1;const[i,r]=o[0],[l,u]=o[o.length-1];if(i!==l||r!==u)for(let h=1;h<e.length;h++){const c=e[h],[p,a]=c[0],[g,B]=c[c.length-1];if(u===a&&l===p){for(t=1;t<c.length;t++)o.push([c[t][0],c[t][1]]);f=!0}else if(u===B&&l===g){for(t=c.length-2;t>=0;t--)o.push([c[t][0],c[t][1]]);f=!0}else if(r===B&&i===g){for(t=0;t<c.length-1;t++)o.splice(t,0,[c[t][0],c[t][1]]);f=!0}else if(r===a&&i===p){for(t=1;t<c.length;t++)o.unshift([c[t][0],c[t][1]]);f=!0}if(f){e.splice(h,1),d(o[0],o[o.length-1])&&(e.shift(),s.getPointsBoundsWidth(o)>0&&n.rings.push(o));break}}f||(e.shift(),o.length,n.rings.push(o))}return n}function p([e],t){return e<t}function a([e,t],[s,n],o){return[o,t+(n-t)/(s-e)*(o-e)]}function g(e,t){let s=(e+t)/(2*t);return s%2==1&&s--,Math.floor(s)}function d([e,t],[s,n]){return e===s&&t===n}e.normalize=f,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
