/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{isISO8601Date as e}from"../../../../core/date.js";import t from"../../../../core/Error.js";import{isWGS84 as o}from"../../../../geometry/support/spatialReferenceUtils.js";import{convertFromMultipoint as n,convertFromPolyline as r}from"../../featureConversionUtils.js";import{OptimizedFeature as i}from"../../OptimizedFeature.js";import s from"../../OptimizedGeometry.js";import{normalizeFieldName as c,isNumericField as l}from"../../../support/fieldUtils.js";const u={LineString:"esriGeometryPolyline",MultiLineString:"esriGeometryPolyline",MultiPoint:"esriGeometryMultipoint",Point:"esriGeometryPoint",Polygon:"esriGeometryPolygon",MultiPolygon:"esriGeometryPolygon"};function f(e){return u[e]}function*p(e){switch(e.type){case"Feature":yield e;break;case"FeatureCollection":for(const t of e.features)t&&(yield t)}}function*a(e){if(e)switch(e.type){case"Point":yield e.coordinates;break;case"LineString":case"MultiPoint":yield*e.coordinates;break;case"MultiLineString":case"Polygon":for(const t of e.coordinates)yield*t;break;case"MultiPolygon":for(const t of e.coordinates)for(const e of t)yield*e}}function*y(e,t={}){const{geometryType:o,objectIdField:n}=t;for(const r of e){const{geometry:e,properties:s,id:c}=r;if(e&&f(e.type)!==o)continue;const l=s||{};let u;n&&(u=l[n],null==c||u||(l[n]=u=c)),yield new i(e&&P(e,t),l,null,u)}}function d(e){for(const t of e)if(t.length>2)return!0;return!1}function g(e){return!h(e)}function m(e){return h(e)}function h(e){let t=0;for(let o=0;o<e.length;o++){const n=e[o],r=e[(o+1)%e.length];t+=n[0]*r[1]-r[0]*n[1]}return t<=0}function w(e){const t=e[0],o=e[e.length-1];t[0]===o[0]&&t[1]===o[1]&&t[2]===o[2]||e.push(t)}function P(e,t){const{coordinates:o,type:i}=e,c=t.hasZ??!1,l=!0;switch(i){case"Point":return new s([],[...o],c,!1);case"LineString":return r({paths:[o],hasZ:l},c,!1);case"MultiLineString":return r({paths:o,hasZ:l},c,!1);case"MultiPoint":return n({points:o,hasZ:l},c,!1);case"Polygon":case"MultiPolygon":{const e=new s([],[],c,!1);for(const t of"Polygon"===i?[o]:o){j(e,t[0],c);for(let o=1;o<t.length;o++)b(e,t[o],c)}return e}}}function j(e,t,o){w(t),g(t)?F(e,t,o):S(e,t,o)}function b(e,t,o){w(t),m(t)?F(e,t,o):S(e,t,o)}function S(e,t,o){for(const n of t)G(e,n,o);e.lengths.push(t.length)}function F(e,t,o){for(let n=t.length-1;n>=0;n--)G(e,t[n],o);e.lengths.push(t.length)}function G(e,[t,o,n],r){e.coords.push(t,o),r&&e.coords.push(n||0)}function k(t){switch(typeof t){case"string":return e(t)?"esriFieldTypeDate":"esriFieldTypeString";case"number":return"esriFieldTypeDouble";default:return"unknown"}}function M(e,n=4326){if(!e)throw new t("geojson-layer:empty","GeoJSON data is empty");if("Feature"!==e.type&&"FeatureCollection"!==e.type)throw new t("geojson-layer:unsupported-geojson-object","missing or not supported GeoJSON object type",{data:e});const{crs:r}=e;if(!r)return;const i="string"==typeof r?r:"name"===r.type?r.properties.name:"EPSG"===r.type?r.properties.code:null,s=o({wkid:n})?new RegExp(".*(CRS84H?|4326)$","i"):new RegExp(`.*(${n})$`,"i");if(!i||!s.test(i))throw new t("geojson:unsupported-crs","unsupported GeoJSON 'crs' member",{crs:r})}function T(e,t={}){const o=[],n=new Set,r=new Set;let i,s=!1,u=null,y=!1,{geometryType:g=null}=t;for(const l of p(e)){const{geometry:e,properties:t,id:p}=l;if(!e||(g||(g=f(e.type)),f(e.type)===g)){if(!s){s=d(a(e))}if(y||(y=null!=p,y&&(i=typeof p,t&&(u=Object.keys(t).filter(e=>t[e]===p)))),t&&u&&y&&null!=p&&(u.length>1?u=u.filter(e=>t[e]===p):1===u.length&&(u=t[u[0]]===p?u:[])),t)for(const e in t){if(n.has(e))continue;const i=k(t[e]);if("unknown"===i){r.add(e);continue}r.delete(e),n.add(e);const s=c(e);s&&o.push({name:s,alias:e,type:i})}}}const m=c(1===u?.length&&u[0]||null)??void 0;if(m)for(const c of o)if(c.name===m&&l(c)){c.type="esriFieldTypeOID";break}return{fields:o,geometryType:g,hasZ:s,objectIdFieldName:m,objectIdFieldType:i,unknownFields:Array.from(r)}}function O(e,t){return Array.from(y(p(e),t))}export{O as createOptimizedFeatures,f as getGeometryType,T as inferLayerProperties,M as validateGeoJSON};
