import ImageState from"../ImageState.js";import ImageStyle from"./Image.js";import{asArray}from"../color.js";import{asColorLike}from"../colorlike.js";import{createCanvasContext2D}from"../dom.js";import{defaultFillStyle,defaultLineCap,defaultLineJoin,defaultLineWidth,defaultMiterLimit,defaultStrokeStyle}from"../render/canvas.js";class RegularShape extends ImageStyle{constructor(t){super({opacity:1,rotateWithView:void 0!==t.rotateWithView&&t.rotateWithView,rotation:void 0!==t.rotation?t.rotation:0,scale:void 0!==t.scale?t.scale:1,displacement:void 0!==t.displacement?t.displacement:[0,0],declutterMode:t.declutterMode}),this.canvases_,this.hitDetectionCanvas_=null,this.fill_=void 0!==t.fill?t.fill:null,this.origin_=[0,0],this.points_=t.points,this.radius=t.radius,this.radius2_=t.radius2,this.angle_=void 0!==t.angle?t.angle:0,this.stroke_=void 0!==t.stroke?t.stroke:null,this.size_,this.renderOptions_,this.imageState_=this.fill_&&this.fill_.loading()?ImageState.LOADING:ImageState.LOADED,this.imageState_===ImageState.LOADING&&this.ready().then(()=>this.imageState_=ImageState.LOADED),this.render()}clone(){const t=this.getScale(),e=new RegularShape({fill:this.getFill()?this.getFill().clone():void 0,points:this.getPoints(),radius:this.getRadius(),radius2:this.getRadius2(),angle:this.getAngle(),stroke:this.getStroke()?this.getStroke().clone():void 0,rotation:this.getRotation(),rotateWithView:this.getRotateWithView(),scale:Array.isArray(t)?t.slice():t,displacement:this.getDisplacement().slice(),declutterMode:this.getDeclutterMode()});return e.setOpacity(this.getOpacity()),e}getAnchor(){var t=this.size_,e=this.getDisplacement(),i=this.getScaleArray();return[t[0]/2-e[0]/i[0],t[1]/2+e[1]/i[1]]}getAngle(){return this.angle_}getFill(){return this.fill_}setFill(t){this.fill_=t,this.render()}getHitDetectionImage(){return this.hitDetectionCanvas_||(this.hitDetectionCanvas_=this.createHitDetectionCanvas_(this.renderOptions_)),this.hitDetectionCanvas_}getImage(t){let e=this.canvases_[t];var i,s;return e||(i=this.renderOptions_,s=createCanvasContext2D(i.size*t,i.size*t),this.draw_(i,s,t),e=s.canvas,this.canvases_[t]=e),e}getPixelRatio(t){return t}getImageSize(){return this.size_}getImageState(){return this.imageState_}getOrigin(){return this.origin_}getPoints(){return this.points_}getRadius(){return this.radius}getRadius2(){return this.radius2_}getSize(){return this.size_}getStroke(){return this.stroke_}setStroke(t){this.stroke_=t,this.render()}listenImageChange(t){}load(){}unlistenImageChange(t){}calculateLineJoinSize_(t,e,i){if(0===e||this.points_===1/0||"bevel"!==t&&"miter"!==t)return e;let s=this.radius,a=void 0===this.radius2_?s:this.radius2_;s<a&&(r=s,s=a,a=r);var r=void 0===this.radius2_?this.points_:2*this.points_,r=2*Math.PI/r,l=a*Math.sin(r),n=Math.sqrt(a*a-l*l),n=s-n,h=Math.sqrt(l*l+n*n),l=h/l;if("miter"===t&&l<=i)return l*e;l=e/2/l,n=e/2*(n/h),h=Math.sqrt((s+l)*(s+l)+n*n)-s;if(void 0===this.radius2_||"bevel"===t)return 2*h;l=s*Math.sin(r),n=Math.sqrt(s*s-l*l),t=a-n,r=Math.sqrt(l*l+t*t)/l;return r<=i?(n=r*e/2-a-s,2*Math.max(h,n)):2*h}createRenderOptions(){let t=defaultLineCap,e=defaultLineJoin,i=0,s=null,a=0,r,l=0;this.stroke_&&(r=asColorLike(this.stroke_.getColor()??defaultStrokeStyle),l=this.stroke_.getWidth()??defaultLineWidth,s=this.stroke_.getLineDash(),a=this.stroke_.getLineDashOffset()??0,e=this.stroke_.getLineJoin()??defaultLineJoin,t=this.stroke_.getLineCap()??defaultLineCap,i=this.stroke_.getMiterLimit()??defaultMiterLimit);var n=this.calculateLineJoinSize_(e,l,i),h=Math.max(this.radius,this.radius2_||0),h=Math.ceil(2*h+n);return{strokeStyle:r,strokeWidth:l,size:h,lineCap:t,lineDash:s,lineDashOffset:a,lineJoin:e,miterLimit:i}}render(){this.renderOptions_=this.createRenderOptions();var t=this.renderOptions_.size;this.canvases_={},this.hitDetectionCanvas_=null,this.size_=[t,t]}draw_(t,e,i){if(e.scale(i,i),e.translate(t.size/2,t.size/2),this.createPath_(e),this.fill_){let t=this.fill_.getColor();null===t&&(t=defaultFillStyle),e.fillStyle=asColorLike(t),e.fill()}t.strokeStyle&&(e.strokeStyle=t.strokeStyle,e.lineWidth=t.strokeWidth,t.lineDash&&(e.setLineDash(t.lineDash),e.lineDashOffset=t.lineDashOffset),e.lineCap=t.lineCap,e.lineJoin=t.lineJoin,e.miterLimit=t.miterLimit,e.stroke())}createHitDetectionCanvas_(i){let s;if(this.fill_){let t=this.fill_.getColor(),e=0;null===(t="string"==typeof t?asArray(t):t)?e=1:Array.isArray(t)&&(e=4===t.length?t[3]:1),0===e&&(s=createCanvasContext2D(i.size,i.size),this.drawHitDetectionCanvas_(i,s))}return s?s.canvas:this.getImage(1)}createPath_(e){let i=this.points_;var s=this.radius;if(i===1/0)e.arc(0,0,s,0,2*Math.PI);else{var a=void 0===this.radius2_?s:this.radius2_,r=(void 0!==this.radius2_&&(i*=2),this.angle_-Math.PI/2),l=2*Math.PI/i;for(let t=0;t<i;t++){var n=r+t*l,h=t%2==0?s:a;e.lineTo(h*Math.cos(n),h*Math.sin(n))}e.closePath()}}drawHitDetectionCanvas_(t,e){e.translate(t.size/2,t.size/2),this.createPath_(e),e.fillStyle=defaultFillStyle,e.fill(),t.strokeStyle&&(e.strokeStyle=t.strokeStyle,e.lineWidth=t.strokeWidth,t.lineDash&&(e.setLineDash(t.lineDash),e.lineDashOffset=t.lineDashOffset),e.lineJoin=t.lineJoin,e.miterLimit=t.miterLimit,e.stroke())}ready(){return this.fill_?this.fill_.ready():Promise.resolve()}}export default RegularShape;