import{j as e}from"./vendor-mui-69397583.js";import{r as a}from"./vendor-react-ece1c85f.js";import{S as R}from"./index-b028e652.js";import{U as D}from"./Upload-fb34ce03.js";import{ab as F,B as P,bZ as N,Y as E,l as T}from"./index-c62ac197.js";import{S as O}from"./StickyFooter-5b934274.js";import{a as M}from"./index-e4c6057f.js";import{r as _,u as B,s as L}from"./index-f1576468.js";const H=n=>{F.getState();const i=a.useRef({}),[x,p]=a.useState(""),[g,o]=a.useState("Draw"),[A,h]=a.useState(null),b=1,y=5e5;a.useState("");const w=()=>i.current.clear();a.useState(""),a.useState("");const f=(s,t="image.png")=>{const[r,c]=s.split(","),u=r.match(/:(.*?);/),S=u?u[1]:"image/png",d=atob(c),m=new Uint8Array(d.length);for(let j=0;j<d.length;j++)m[j]=d.charCodeAt(j);const U=new Blob([m],{type:S});return new File([U],t,{type:S})},l=()=>{if(i.current.isEmpty())alert("Please provide a signature first.");else{const s=i.current.toDataURL("image/png");p(s);const t="myImage.png";new File([s],t,{type:"image/png"});const r=f(s);h(r)}},C=(s,t)=>{let r=!0;if(t.length>=b)return`You can only upload ${b} file(s)`;if(s)for(const c of s)c.size>=y&&(r="Upload file cannot more then 500kb!");return r},I=()=>{n.onSignSubmit(A,x),n.onDrawerClose()};return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"signature-container",children:[g=="Draw"&&e.jsxs("div",{children:[e.jsx("button",{onClick:()=>o("Upload"),children:"Upload"}),e.jsx("br",{}),e.jsx(R,{ref:i,penColor:"black",canvasProps:{width:500,height:200,className:"signature-canvas"}}),e.jsxs("div",{className:"buttons",children:[e.jsx("button",{onClick:w,children:"Clear"}),e.jsx("button",{onClick:l,children:"Save"})]})]}),g=="Upload"&&e.jsxs("div",{children:[e.jsx("button",{onClick:()=>o("Draw"),children:"Sign"}),e.jsx("br",{}),e.jsx(D,{draggable:!0,cropNeeded:!1,beforeUpload:C,onChange:s=>{p(URL.createObjectURL(s[0])),h(s[0])}})]})]}),x!=""&&e.jsxs("div",{children:[e.jsx("br",{}),e.jsx("br",{}),e.jsx("div",{children:e.jsx("h3",{children:"Preview"})}),e.jsx("div",{children:e.jsx("img",{src:x})})]}),e.jsx(O,{className:"px-8 flex items-center justify-between py-4",stickyClass:"bg-white dark:bg-gray-800 border-gray-200 dark:border-gray-700",children:e.jsx("div",{className:"md:flex items-center justify-between w-full",children:e.jsx(P,{size:"sm",variant:"solid",onClick:I,icon:e.jsx(M,{}),type:"button",children:"Add"})})})]})};async function J(n){return(await N.apiV10UserSignatureAddSignaturePost(n==null?void 0:n.signatureName,n.signatureFile,n.userId)).data}async function K(n){return(await N.apiV10UserSignatureAddAnonmousUserSignaturePost(n==null?void 0:n.signatureName,n.signatureFile,n.token)).data}E("TimeLogData",_);const Q=n=>{console.log("avaialblesignatureUrl",n),F.getState();const i=a.useRef({}),[x,p]=a.useState(""),[g,o]=a.useState("Draw"),[A,h]=a.useState(null),b=1,y=5e5,w=()=>i.current.clear(),f=T(),l=B(t=>t.TimeLogData.data.signatureImageUrl);console.log("signatureImageUrl",l),a.useEffect(()=>{console.log("INSIde UseEffect - signatureImageUrl",n.Id,l),l!=""?o("preview"):o("Draw"),console.log("avaialblesignatureUrl",n)},[l]);const C=(t,r="image.png")=>{const[c,u]=t.split(","),S=c.match(/:(.*?);/),d=S?S[1]:"image/png",m=atob(u),U=new Uint8Array(m.length);for(let v=0;v<m.length;v++)U[v]=m.charCodeAt(v);const k=new Blob([U],{type:d});return new File([k],r,{type:d})},I=()=>{if(i.current.isEmpty())alert("Please provide a signature first.");else{const t=i.current.toDataURL("image/png");console.log("Saved Signature:",t),p(t);const r=C(t);h(r),o("preview"),n.onSignSubmit(r,t),f(L(t))}},s=(t,r)=>{let c=!0;if(r.length>=b)return`You can only upload ${b} file(s)`;if(t)for(const u of t)u.size>=y&&(c="Upload file cannot more then 500kb!");return c};return e.jsx(e.Fragment,{children:e.jsxs("div",{className:"signature-container",style:{marginBottom:"0.5rem"},children:[g=="Draw"&&e.jsxs("div",{children:[e.jsxs("div",{style:{display:"flex",position:"relative",width:"50%"},children:[e.jsx("div",{children:e.jsx("button",{onClick:()=>o("Upload"),children:"Upload"})}),e.jsxs("div",{className:"buttons",style:{marginTop:"0",position:"absolute",right:"0"},children:[e.jsx("button",{onClick:w,children:"Clear"}),e.jsx("button",{onClick:I,children:"Save"})]})]}),e.jsx("hr",{style:{width:"50%"}}),e.jsx(R,{ref:i,penColor:"black",canvasProps:{height:100,className:"signature-canvas"}})]}),g=="Upload"&&e.jsxs("div",{children:[e.jsx("button",{onClick:()=>o("Draw"),children:"Sign"}),e.jsx("hr",{}),e.jsx(D,{draggable:!0,cropNeeded:!1,beforeUpload:s,onChange:t=>{console.log("files: ",t),p(URL.createObjectURL(t[0])),h(t[0]),o("preview"),f(L(URL.createObjectURL(t[0]))),n.onSignSubmit(t[0],URL.createObjectURL(t[0]))}})]}),g=="preview"&&l!=""&&e.jsxs("div",{children:[e.jsx("button",{onClick:()=>o("Draw"),children:"Sign"})," |   ",e.jsx("button",{onClick:()=>o("Upload"),children:"Upload"}),e.jsx("hr",{}),e.jsx("br",{}),e.jsx("img",{src:l})]})]})})};export{H as A,Q as I,K as a,J as b};
