import{__decorate as t}from"tslib";import{html as i}from"lit";import{property as e,query as r,customElement as s}from"lit/decorators.js";import{s as a}from"./nile-qr-code.css.esm.js";import{g as n}from"./nile-qr-code-utils.esm.js";import{N as o}from"../internal/nile-element.esm.js";import"../internal/accessibility/a11y.state.enum.esm.js";import"../internal/accessibility/a11y.property.enum.esm.js";import"../internal/accessibility/role.enum.esm.js";let l=class extends o{constructor(){super(...arguments),this.value="",this.size=128,this.fill="black",this.background="white",this.radius=0,this.errorCorrection="H",this.label="",this.image="",this.imageSize=.25,this.imagePadding=4,this.imageRadius=4,this.fillGradient="",this._loadedImage=null,this._imageLoadPromise=null}static get styles(){return[a]}updated(t){if(super.updated(t),t.has("image")&&(this._loadedImage=null,this._imageLoadPromise=null,this.image))return void this.loadImage(this.image);["value","size","fill","background","radius","errorCorrection","imageSize","imagePadding","imageRadius","fillGradient"].some((i=>t.has(i)))&&this.drawQrCode()}render(){const t=this.label||this.value||"QR Code";return i`
      <canvas
        part="base"
        role="img"
        aria-label=${t}
        width=${this.size}
        height=${this.size}
      ></canvas>
    `}download(t="qr-code.png"){if(!this.canvas)return;const i=document.createElement("a");i.download=t,i.href=this.canvas.toDataURL("image/png"),i.click()}toDataURL(t="image/png"){return this.canvas?this.canvas.toDataURL(t):""}loadImage(t){const i=new Image;i.crossOrigin="anonymous",this._imageLoadPromise=new Promise((e=>{i.onload=()=>{this._loadedImage=i,this._imageLoadPromise=null,this.drawQrCode(),e()},i.onerror=()=>{this._loadedImage=null,this._imageLoadPromise=null,this.emit("nile-qr-image-error",{src:t}),this.drawQrCode(),e()}})),i.src=t}drawQrCode(){if(!this.canvas)return;const t=this.canvas.getContext("2d");if(!t)return;const{size:i,fill:e,background:r,radius:s,errorCorrection:a,value:o}=this,l=Math.max(0,Math.min(.5,s)),h=n(o,a),d=h.length,c=i/d;t.clearRect(0,0,i,i),r&&"transparent"!==r&&(t.fillStyle=r,t.fillRect(0,0,i,i));const u=Math.max(.1,Math.min(.4,this.imageSize)),m=this._loadedImage?i*u:0,f=m+(this._loadedImage?2*this.imagePadding:0),g=(i-f)/2,p=g+f,b=this.resolveModuleFill(t,i);t.fillStyle=b;for(let i=0;i<d;i++)for(let e=0;e<d;e++){if(1!==h[i][e])continue;const r=e*c,s=i*c;if(this._loadedImage){if(r>=g&&r+c<=p&&s>=g&&s+c<=p)continue}l>0?this.drawRoundedModule(t,r,s,c,c,l*c):t.fillRect(r,s,c,c)}this._loadedImage&&this.drawCenterImage(t,i,m)}resolveModuleFill(t,i){if(!this.fillGradient)return this.fill;const e=this.fillGradient.split(",").map((t=>t.trim()));if(e.length<3)return this.fill;const r=parseFloat(e[0]);if(Number.isNaN(r))return this.fill;const s=r*Math.PI/180,a=i/2,n=i/2,o=i/2,l=a-Math.cos(s)*o,h=n-Math.sin(s)*o,d=a+Math.cos(s)*o,c=n+Math.sin(s)*o,u=t.createLinearGradient(l,h,d,c),m=e.slice(1);for(let t=0;t<m.length;t++)u.addColorStop(t/(m.length-1),m[t]);return u}drawCenterImage(t,i,e){if(!this._loadedImage)return;const r=(i-e)/2,s=(i-e)/2,a=Math.min(this.imageRadius,e/2),n=this.imagePadding;if(n>0&&this.background&&"transparent"!==this.background){t.fillStyle=this.background;const i=r-n,o=s-n,l=e+2*n,h=e+2*n,d=a+.5*n;this.drawRoundedRect(t,i,o,l,h,d),t.fill()}t.save(),this.clipRoundedRect(t,r,s,e,e,a),t.drawImage(this._loadedImage,r,s,e,e),t.restore()}drawRoundedModule(t,i,e,r,s,a){t.beginPath(),t.moveTo(i+a,e),t.lineTo(i+r-a,e),t.arcTo(i+r,e,i+r,e+a,a),t.lineTo(i+r,e+s-a),t.arcTo(i+r,e+s,i+r-a,e+s,a),t.lineTo(i+a,e+s),t.arcTo(i,e+s,i,e+s-a,a),t.lineTo(i,e+a),t.arcTo(i,e,i+a,e,a),t.closePath(),t.fill()}drawRoundedRect(t,i,e,r,s,a){t.beginPath(),t.moveTo(i+a,e),t.lineTo(i+r-a,e),t.arcTo(i+r,e,i+r,e+a,a),t.lineTo(i+r,e+s-a),t.arcTo(i+r,e+s,i+r-a,e+s,a),t.lineTo(i+a,e+s),t.arcTo(i,e+s,i,e+s-a,a),t.lineTo(i,e+a),t.arcTo(i,e,i+a,e,a),t.closePath()}clipRoundedRect(t,i,e,r,s,a){t.beginPath(),t.moveTo(i+a,e),t.lineTo(i+r-a,e),t.arcTo(i+r,e,i+r,e+a,a),t.lineTo(i+r,e+s-a),t.arcTo(i+r,e+s,i+r-a,e+s,a),t.lineTo(i+a,e+s),t.arcTo(i,e+s,i,e+s-a,a),t.lineTo(i,e+a),t.arcTo(i,e,i+a,e,a),t.closePath(),t.clip()}};t([e({type:String,reflect:!0})],l.prototype,"value",void 0),t([e({type:Number,reflect:!0})],l.prototype,"size",void 0),t([e({type:String,reflect:!0})],l.prototype,"fill",void 0),t([e({type:String,reflect:!0})],l.prototype,"background",void 0),t([e({type:Number,reflect:!0})],l.prototype,"radius",void 0),t([e({attribute:"error-correction",reflect:!0})],l.prototype,"errorCorrection",void 0),t([e({type:String,reflect:!0})],l.prototype,"label",void 0),t([e({type:String,reflect:!0})],l.prototype,"image",void 0),t([e({attribute:"image-size",type:Number,reflect:!0})],l.prototype,"imageSize",void 0),t([e({attribute:"image-padding",type:Number,reflect:!0})],l.prototype,"imagePadding",void 0),t([e({attribute:"image-radius",type:Number,reflect:!0})],l.prototype,"imageRadius",void 0),t([e({attribute:"fill-gradient",type:String,reflect:!0})],l.prototype,"fillGradient",void 0),t([r("canvas")],l.prototype,"canvas",void 0),l=t([s("nile-qr-code")],l);export{l as N};
