import WebGLArrayBuffer from"../../webgl/Buffer.js";import{ARRAY_BUFFER,DYNAMIC_DRAW,ELEMENT_ARRAY_BUFFER}from"../../webgl.js";import{create as createTransform}from"../../transform.js";import{getUid}from"../../util.js";class MixedGeometryBatch{constructor(){this.polygonBatch={entries:{},geometriesCount:0,verticesCount:0,ringsCount:0,renderInstructions:new Float32Array(0),verticesBuffer:new WebGLArrayBuffer(ARRAY_BUFFER,DYNAMIC_DRAW),indicesBuffer:new WebGLArrayBuffer(ELEMENT_ARRAY_BUFFER,DYNAMIC_DRAW),renderInstructionsTransform:createTransform(),verticesBufferTransform:createTransform(),invertVerticesBufferTransform:createTransform()},this.pointBatch={entries:{},geometriesCount:0,renderInstructions:new Float32Array(0),verticesBuffer:new WebGLArrayBuffer(ARRAY_BUFFER,DYNAMIC_DRAW),indicesBuffer:new WebGLArrayBuffer(ELEMENT_ARRAY_BUFFER,DYNAMIC_DRAW),renderInstructionsTransform:createTransform(),verticesBufferTransform:createTransform(),invertVerticesBufferTransform:createTransform()},this.lineStringBatch={entries:{},geometriesCount:0,verticesCount:0,renderInstructions:new Float32Array(0),verticesBuffer:new WebGLArrayBuffer(ARRAY_BUFFER,DYNAMIC_DRAW),indicesBuffer:new WebGLArrayBuffer(ELEMENT_ARRAY_BUFFER,DYNAMIC_DRAW),renderInstructionsTransform:createTransform(),verticesBufferTransform:createTransform(),invertVerticesBufferTransform:createTransform()}}addFeatures(e){for(let t=0;t<e.length;t++)this.addFeature(e[t])}addFeature(t){var e=t.getGeometry();e&&this.addGeometry_(e,t)}addFeatureEntryInPointBatch_(t){var e=getUid(t);return e in this.pointBatch.entries||(this.pointBatch.entries[e]={feature:t,flatCoordss:[]}),this.pointBatch.entries[e]}addFeatureEntryInLineStringBatch_(t){var e=getUid(t);return e in this.lineStringBatch.entries||(this.lineStringBatch.entries[e]={feature:t,flatCoordss:[],verticesCount:0}),this.lineStringBatch.entries[e]}addFeatureEntryInPolygonBatch_(t){var e=getUid(t);return e in this.polygonBatch.entries||(this.polygonBatch.entries[e]={feature:t,flatCoordss:[],verticesCount:0,ringsCount:0,ringsVerticesCounts:[]}),this.polygonBatch.entries[e]}clearFeatureEntryInPointBatch_(t){var e=this.pointBatch.entries[getUid(t)];e&&(this.pointBatch.geometriesCount-=e.flatCoordss.length,delete this.pointBatch.entries[getUid(t)])}clearFeatureEntryInLineStringBatch_(t){var e=this.lineStringBatch.entries[getUid(t)];e&&(this.lineStringBatch.verticesCount-=e.verticesCount,this.lineStringBatch.geometriesCount-=e.flatCoordss.length,delete this.lineStringBatch.entries[getUid(t)])}clearFeatureEntryInPolygonBatch_(t){var e=this.polygonBatch.entries[getUid(t)];e&&(this.polygonBatch.verticesCount-=e.verticesCount,this.polygonBatch.ringsCount-=e.ringsCount,this.polygonBatch.geometriesCount-=e.flatCoordss.length,delete this.polygonBatch.entries[getUid(t)])}addGeometry_(t,e){let r,n,i;switch(t.getType()){case"GeometryCollection":t.getGeometries().map(t=>this.addGeometry_(t,e));break;case"MultiPolygon":t.getPolygons().map(t=>this.addGeometry_(t,e));break;case"MultiLineString":t.getLineStrings().map(t=>this.addGeometry_(t,e));break;case"MultiPoint":t.getPoints().map(t=>this.addGeometry_(t,e));break;case"Polygon":const o=t;i=this.addFeatureEntryInPolygonBatch_(e),r=o.getFlatCoordinates(),n=r.length/2;var s=o.getLinearRingCount(),a=o.getEnds().map((t,e,r)=>0<e?(t-r[e-1])/2:t/2);this.polygonBatch.verticesCount+=n,this.polygonBatch.ringsCount+=s,this.polygonBatch.geometriesCount++,i.flatCoordss.push(r),i.ringsVerticesCounts.push(a),i.verticesCount+=n,i.ringsCount+=s,o.getLinearRings().map(t=>this.addGeometry_(t,e));break;case"Point":const c=t;i=this.addFeatureEntryInPointBatch_(e),r=c.getFlatCoordinates(),this.pointBatch.geometriesCount++,i.flatCoordss.push(r);break;case"LineString":case"LinearRing":const h=t;i=this.addFeatureEntryInLineStringBatch_(e),r=h.getFlatCoordinates(),n=r.length/2,this.lineStringBatch.verticesCount+=n,this.lineStringBatch.geometriesCount++,i.flatCoordss.push(r),i.verticesCount+=n}}changeFeature(t){this.clearFeatureEntryInPointBatch_(t),this.clearFeatureEntryInPolygonBatch_(t),this.clearFeatureEntryInLineStringBatch_(t);var e=t.getGeometry();e&&this.addGeometry_(e,t)}removeFeature(t){this.clearFeatureEntryInPointBatch_(t),this.clearFeatureEntryInPolygonBatch_(t),this.clearFeatureEntryInLineStringBatch_(t)}clear(){this.polygonBatch.entries={},this.polygonBatch.geometriesCount=0,this.polygonBatch.verticesCount=0,this.polygonBatch.ringsCount=0,this.lineStringBatch.entries={},this.lineStringBatch.geometriesCount=0,this.lineStringBatch.verticesCount=0,this.pointBatch.entries={},this.pointBatch.geometriesCount=0}}export default MixedGeometryBatch;