var __extends=this&&this.__extends||function(){var s=function(t,e){return(s=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])})(t,e)};return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function i(){this.constructor=t}s(t,e),t.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)}}();import GeometryType from"../../geom/GeometryType.js";import VectorContext from"../VectorContext.js";import{asColorLike}from"../../colorlike.js";import{compose as composeTransform,create as createTransform}from"../../transform.js";import{defaultFillStyle,defaultFont,defaultLineCap,defaultLineDash,defaultLineDashOffset,defaultLineJoin,defaultLineWidth,defaultMiterLimit,defaultStrokeStyle,defaultTextAlign,defaultTextBaseline}from"../canvas.js";import{equals}from"../../array.js";import{intersects}from"../../extent.js";import{transform2D}from"../../geom/flat/transform.js";import{transformGeom2D}from"../../geom/SimpleGeometry.js";var CanvasImmediateRenderer=function(l){function t(t,e,i,s,a,o,n){var r=l.call(this)||this;return r.context_=t,r.pixelRatio_=e,r.extent_=i,r.transform_=s,r.viewRotation_=a,r.squaredTolerance_=o,r.userTransform_=n,r.contextFillState_=null,r.contextStrokeState_=null,r.contextTextState_=null,r.fillState_=null,r.strokeState_=null,r.image_=null,r.imageAnchorX_=0,r.imageAnchorY_=0,r.imageHeight_=0,r.imageOpacity_=0,r.imageOriginX_=0,r.imageOriginY_=0,r.imageRotateWithView_=!1,r.imageRotation_=0,r.imageScale_=[0,0],r.imageWidth_=0,r.text_="",r.textOffsetX_=0,r.textOffsetY_=0,r.textRotateWithView_=!1,r.textRotation_=0,r.textScale_=[0,0],r.textFillState_=null,r.textStrokeState_=null,r.textState_=null,r.pixelCoordinates_=[],r.tmpLocalTransform_=createTransform(),r}return __extends(t,l),t.prototype.drawImages_=function(t,e,i,s){if(this.image_){var a=transform2D(t,e,i,s,this.transform_,this.pixelCoordinates_),o=this.context_,n=this.tmpLocalTransform_,t=o.globalAlpha,r=(1!=this.imageOpacity_&&(o.globalAlpha=t*this.imageOpacity_),this.imageRotation_);this.imageRotateWithView_&&(r+=this.viewRotation_);for(var l=0,h=a.length;l<h;l+=2){var _,f,m=a[l]-this.imageAnchorX_,g=a[l+1]-this.imageAnchorY_;0!==r||1!=this.imageScale_[0]||1!=this.imageScale_[1]?(_=m+this.imageAnchorX_,f=g+this.imageAnchorY_,composeTransform(n,_,f,1,1,r,-_,-f),o.setTransform.apply(o,n),o.translate(_,f),o.scale(this.imageScale_[0],this.imageScale_[1]),o.drawImage(this.image_,this.imageOriginX_,this.imageOriginY_,this.imageWidth_,this.imageHeight_,-this.imageAnchorX_,-this.imageAnchorY_,this.imageWidth_,this.imageHeight_),o.setTransform(1,0,0,1,0,0)):o.drawImage(this.image_,this.imageOriginX_,this.imageOriginY_,this.imageWidth_,this.imageHeight_,m,g,this.imageWidth_,this.imageHeight_)}1!=this.imageOpacity_&&(o.globalAlpha=t)}},t.prototype.drawText_=function(t,e,i,s){if(this.textState_&&""!==this.text_){this.textFillState_&&this.setContextFillState_(this.textFillState_),this.textStrokeState_&&this.setContextStrokeState_(this.textStrokeState_),this.setContextTextState_(this.textState_);var a=transform2D(t,e,i,s,this.transform_,this.pixelCoordinates_),o=this.context_,n=this.textRotation_;for(this.textRotateWithView_&&(n+=this.viewRotation_);e<i;e+=s){var r,l=a[e]+this.textOffsetX_,h=a[e+1]+this.textOffsetY_;0!==n||1!=this.textScale_[0]||1!=this.textScale_[1]?(r=composeTransform(this.tmpLocalTransform_,l,h,1,1,n,-l,-h),o.setTransform.apply(o,r),o.translate(l,h),o.scale(this.textScale_[0],this.textScale_[1]),this.textStrokeState_&&o.strokeText(this.text_,0,0),this.textFillState_&&o.fillText(this.text_,0,0),o.setTransform(1,0,0,1,0,0)):(this.textStrokeState_&&o.strokeText(this.text_,l,h),this.textFillState_&&o.fillText(this.text_,l,h))}}},t.prototype.moveToLineTo_=function(t,e,i,s,a){var o=this.context_,n=transform2D(t,e,i,s,this.transform_,this.pixelCoordinates_),r=(o.moveTo(n[0],n[1]),n.length);a&&(r-=2);for(var l=2;l<r;l+=2)o.lineTo(n[l],n[l+1]);return a&&o.closePath(),i},t.prototype.drawRings_=function(t,e,i,s){for(var a=0,o=i.length;a<o;++a)e=this.moveToLineTo_(t,e,i[a],s,!0);return e},t.prototype.drawCircle=function(t){var e,i,s;intersects(this.extent_,t.getExtent())&&((this.fillState_||this.strokeState_)&&(this.fillState_&&this.setContextFillState_(this.fillState_),this.strokeState_&&this.setContextStrokeState_(this.strokeState_),i=(e=transformGeom2D(t,this.transform_,this.pixelCoordinates_))[2]-e[0],s=e[3]-e[1],i=Math.sqrt(i*i+s*s),(s=this.context_).beginPath(),s.arc(e[0],e[1],i,0,2*Math.PI),this.fillState_&&s.fill(),this.strokeState_&&s.stroke()),""!==this.text_&&this.drawText_(t.getCenter(),0,2,2))},t.prototype.setStyle=function(t){this.setFillStrokeStyle(t.getFill(),t.getStroke()),this.setImageStyle(t.getImage()),this.setTextStyle(t.getText())},t.prototype.setTransform=function(t){this.transform_=t},t.prototype.drawGeometry=function(t){switch(t.getType()){case GeometryType.POINT:this.drawPoint(t);break;case GeometryType.LINE_STRING:this.drawLineString(t);break;case GeometryType.POLYGON:this.drawPolygon(t);break;case GeometryType.MULTI_POINT:this.drawMultiPoint(t);break;case GeometryType.MULTI_LINE_STRING:this.drawMultiLineString(t);break;case GeometryType.MULTI_POLYGON:this.drawMultiPolygon(t);break;case GeometryType.GEOMETRY_COLLECTION:this.drawGeometryCollection(t);break;case GeometryType.CIRCLE:this.drawCircle(t)}},t.prototype.drawFeature=function(t,e){t=e.getGeometryFunction()(t);t&&intersects(this.extent_,t.getExtent())&&(this.setStyle(e),this.drawGeometry(t))},t.prototype.drawGeometryCollection=function(t){for(var e=t.getGeometriesArray(),i=0,s=e.length;i<s;++i)this.drawGeometry(e[i])},t.prototype.drawPoint=function(t){var e=(t=this.squaredTolerance_?t.simplifyTransformed(this.squaredTolerance_,this.userTransform_):t).getFlatCoordinates(),t=t.getStride();this.image_&&this.drawImages_(e,0,e.length,t),""!==this.text_&&this.drawText_(e,0,e.length,t)},t.prototype.drawMultiPoint=function(t){var e=(t=this.squaredTolerance_?t.simplifyTransformed(this.squaredTolerance_,this.userTransform_):t).getFlatCoordinates(),t=t.getStride();this.image_&&this.drawImages_(e,0,e.length,t),""!==this.text_&&this.drawText_(e,0,e.length,t)},t.prototype.drawLineString=function(t){var e,i;this.squaredTolerance_&&(t=t.simplifyTransformed(this.squaredTolerance_,this.userTransform_)),intersects(this.extent_,t.getExtent())&&(this.strokeState_&&(this.setContextStrokeState_(this.strokeState_),e=this.context_,i=t.getFlatCoordinates(),e.beginPath(),this.moveToLineTo_(i,0,i.length,t.getStride(),!1),e.stroke()),""!==this.text_&&(i=t.getFlatMidpoint(),this.drawText_(i,0,2,2)))},t.prototype.drawMultiLineString=function(t){var e=(t=this.squaredTolerance_?t.simplifyTransformed(this.squaredTolerance_,this.userTransform_):t).getExtent();if(intersects(this.extent_,e)){if(this.strokeState_){this.setContextStrokeState_(this.strokeState_);var e=this.context_,i=t.getFlatCoordinates(),s=0,a=t.getEnds(),o=t.getStride();e.beginPath();for(var n=0,r=a.length;n<r;++n)s=this.moveToLineTo_(i,s,a[n],o,!1);e.stroke()}""!==this.text_&&(e=t.getFlatMidpoints(),this.drawText_(e,0,e.length,2))}},t.prototype.drawPolygon=function(t){var e;this.squaredTolerance_&&(t=t.simplifyTransformed(this.squaredTolerance_,this.userTransform_)),intersects(this.extent_,t.getExtent())&&((this.strokeState_||this.fillState_)&&(this.fillState_&&this.setContextFillState_(this.fillState_),this.strokeState_&&this.setContextStrokeState_(this.strokeState_),(e=this.context_).beginPath(),this.drawRings_(t.getOrientedFlatCoordinates(),0,t.getEnds(),t.getStride()),this.fillState_&&e.fill(),this.strokeState_&&e.stroke()),""!==this.text_&&(e=t.getFlatInteriorPoint(),this.drawText_(e,0,2,2)))},t.prototype.drawMultiPolygon=function(t){if(this.squaredTolerance_&&(t=t.simplifyTransformed(this.squaredTolerance_,this.userTransform_)),intersects(this.extent_,t.getExtent())){if(this.strokeState_||this.fillState_){this.fillState_&&this.setContextFillState_(this.fillState_),this.strokeState_&&this.setContextStrokeState_(this.strokeState_);var e=this.context_,i=t.getOrientedFlatCoordinates(),s=0,a=t.getEndss(),o=t.getStride();e.beginPath();for(var n=0,r=a.length;n<r;++n)var l=a[n],s=this.drawRings_(i,s,l,o);this.fillState_&&e.fill(),this.strokeState_&&e.stroke()}""!==this.text_&&(e=t.getFlatInteriorPoints(),this.drawText_(e,0,e.length,2))}},t.prototype.setContextFillState_=function(t){var e=this.context_,i=this.contextFillState_;i?i.fillStyle!=t.fillStyle&&(i.fillStyle=t.fillStyle,e.fillStyle=t.fillStyle):(e.fillStyle=t.fillStyle,this.contextFillState_={fillStyle:t.fillStyle})},t.prototype.setContextStrokeState_=function(t){var e=this.context_,i=this.contextStrokeState_;i?(i.lineCap!=t.lineCap&&(i.lineCap=t.lineCap,e.lineCap=t.lineCap),e.setLineDash&&(equals(i.lineDash,t.lineDash)||e.setLineDash(i.lineDash=t.lineDash),i.lineDashOffset!=t.lineDashOffset&&(i.lineDashOffset=t.lineDashOffset,e.lineDashOffset=t.lineDashOffset)),i.lineJoin!=t.lineJoin&&(i.lineJoin=t.lineJoin,e.lineJoin=t.lineJoin),i.lineWidth!=t.lineWidth&&(i.lineWidth=t.lineWidth,e.lineWidth=t.lineWidth),i.miterLimit!=t.miterLimit&&(i.miterLimit=t.miterLimit,e.miterLimit=t.miterLimit),i.strokeStyle!=t.strokeStyle&&(i.strokeStyle=t.strokeStyle,e.strokeStyle=t.strokeStyle)):(e.lineCap=t.lineCap,e.setLineDash&&(e.setLineDash(t.lineDash),e.lineDashOffset=t.lineDashOffset),e.lineJoin=t.lineJoin,e.lineWidth=t.lineWidth,e.miterLimit=t.miterLimit,e.strokeStyle=t.strokeStyle,this.contextStrokeState_={lineCap:t.lineCap,lineDash:t.lineDash,lineDashOffset:t.lineDashOffset,lineJoin:t.lineJoin,lineWidth:t.lineWidth,miterLimit:t.miterLimit,strokeStyle:t.strokeStyle})},t.prototype.setContextTextState_=function(t){var e=this.context_,i=this.contextTextState_,s=t.textAlign||defaultTextAlign;i?(i.font!=t.font&&(i.font=t.font,e.font=t.font),i.textAlign!=s&&(i.textAlign=s,e.textAlign=s),i.textBaseline!=t.textBaseline&&(i.textBaseline=t.textBaseline,e.textBaseline=t.textBaseline)):(e.font=t.font,e.textAlign=s,e.textBaseline=t.textBaseline,this.contextTextState_={font:t.font,textAlign:s,textBaseline:t.textBaseline})},t.prototype.setFillStrokeStyle=function(t,e){var i,s,a,o,n,r=this;t?(t=t.getColor(),this.fillState_={fillStyle:asColorLike(t||defaultFillStyle)}):this.fillState_=null,e?(t=e.getColor(),i=e.getLineCap(),n=e.getLineDash(),s=e.getLineDashOffset(),a=e.getLineJoin(),o=e.getWidth(),e=e.getMiterLimit(),n=n||defaultLineDash,this.strokeState_={lineCap:void 0!==i?i:defaultLineCap,lineDash:1===this.pixelRatio_?n:n.map(function(t){return t*r.pixelRatio_}),lineDashOffset:(s||defaultLineDashOffset)*this.pixelRatio_,lineJoin:void 0!==a?a:defaultLineJoin,lineWidth:(void 0!==o?o:defaultLineWidth)*this.pixelRatio_,miterLimit:void 0!==e?e:defaultMiterLimit,strokeStyle:asColorLike(t||defaultStrokeStyle)}):this.strokeState_=null},t.prototype.setImageStyle=function(t){var e,i,s;t&&(e=t.getSize())?(i=t.getAnchor(),s=t.getOrigin(),this.image_=t.getImage(this.pixelRatio_),this.imageAnchorX_=i[0]*this.pixelRatio_,this.imageAnchorY_=i[1]*this.pixelRatio_,this.imageHeight_=e[1]*this.pixelRatio_,this.imageOpacity_=t.getOpacity(),this.imageOriginX_=s[0],this.imageOriginY_=s[1],this.imageRotateWithView_=t.getRotateWithView(),this.imageRotation_=t.getRotation(),this.imageScale_=t.getScaleArray(),this.imageWidth_=e[0]*this.pixelRatio_):this.image_=null},t.prototype.setTextStyle=function(t){var e,i,s,a,o,n,r,l;t?((n=t.getFill())?(n=n.getColor(),this.textFillState_={fillStyle:asColorLike(n||defaultFillStyle)}):this.textFillState_=null,(n=t.getStroke())?(r=n.getColor(),e=n.getLineCap(),i=n.getLineDash(),s=n.getLineDashOffset(),a=n.getLineJoin(),o=n.getWidth(),n=n.getMiterLimit(),this.textStrokeState_={lineCap:void 0!==e?e:defaultLineCap,lineDash:i||defaultLineDash,lineDashOffset:s||defaultLineDashOffset,lineJoin:void 0!==a?a:defaultLineJoin,lineWidth:void 0!==o?o:defaultLineWidth,miterLimit:void 0!==n?n:defaultMiterLimit,strokeStyle:asColorLike(r||defaultStrokeStyle)}):this.textStrokeState_=null,e=t.getFont(),i=t.getOffsetX(),s=t.getOffsetY(),a=t.getRotateWithView(),o=t.getRotation(),n=t.getScaleArray(),r=t.getText(),l=t.getTextAlign(),t=t.getTextBaseline(),this.textState_={font:void 0!==e?e:defaultFont,textAlign:void 0!==l?l:defaultTextAlign,textBaseline:void 0!==t?t:defaultTextBaseline},this.text_=void 0!==r?r:"",this.textOffsetX_=void 0!==i?this.pixelRatio_*i:0,this.textOffsetY_=void 0!==s?this.pixelRatio_*s:0,this.textRotateWithView_=void 0!==a&&a,this.textRotation_=void 0!==o?o:0,this.textScale_=[this.pixelRatio_*n[0],this.pixelRatio_*n[1]]):this.text_=""},t}(VectorContext);export default CanvasImmediateRenderer;