import { h } from "@stencil/core";
const Keyboard = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 8h.01"/><path d="M12 12h.01"/><path d="M14 8h.01"/><path d="M16 12h.01"/><path d="M18 8h.01"/><path d="M6 8h.01"/><path d="M7 16h10"/><path d="M8 12h.01"/><rect width="20" height="16" x="2" y="4" rx="2"/></svg>`;
const Pencil = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z"/><path d="m15 5 4 4"/></svg>`;
const Upload = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10.3 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2v10l-3.1-3.1a2 2 0 0 0-2.814.014L6 21"/><path d="m14 19.5 3-3 3 3"/><path d="M17 22v-5.5"/><circle cx="9" cy="9" r="2"/></svg>`;
/**
 * Display a dialog that allows the user to specify a signature image, either by using a signature-font-generated image
 * based on their full name, or by hand-drawing their signature with a mouse or tablet.
 */
export class VerdocsSignatureDialog {
    constructor() {
        /**
         * Initial signature text
         */
        this.name = '';
        this.fontLoaded = false;
        this.enteredName = '';
        this.mode = 'type';
        // Drawing state
        this.isDrawing = false;
        this.hasDrawnSignature = false;
        // Upload state
        this.hasUploadedImage = false;
        this.uploadedFileName = '';
        this.currentStroke = [];
        this.allStrokes = [];
        this.lastPoint = null;
        this.uploadedImage = null;
        this.handlePointerDown = (e) => {
            e.preventDefault();
            if (!this.canvasElement || !this.drawingContext) {
                return;
            }
            this.isDrawing = true;
            this.hasDrawnSignature = true;
            const point = this.getCanvasCoordinates(e);
            this.currentStroke = [point];
            this.lastPoint = point;
            // Start a new path
            this.drawingContext.beginPath();
            this.drawingContext.moveTo(point.x, point.y);
            // Capture pointer to ensure we get all events even if pointer leaves canvas
            this.canvasElement.setPointerCapture(e.pointerId);
        };
        this.handlePointerMove = (e) => {
            e.preventDefault();
            if (!this.isDrawing || !this.drawingContext || !this.lastPoint) {
                return;
            }
            const point = this.getCanvasCoordinates(e);
            this.currentStroke.push(point);
            // Use quadratic curves for smooth drawing
            // The control point is the last point, and we draw to the midpoint
            const midPoint = {
                x: (this.lastPoint.x + point.x) / 2,
                y: (this.lastPoint.y + point.y) / 2,
            };
            this.drawingContext.quadraticCurveTo(this.lastPoint.x, this.lastPoint.y, midPoint.x, midPoint.y);
            this.drawingContext.stroke();
            this.lastPoint = point;
        };
        this.handlePointerUp = (e) => {
            e.preventDefault();
            if (!this.isDrawing || !this.drawingContext) {
                return;
            }
            // Complete the stroke
            if (this.lastPoint && this.currentStroke.length > 0) {
                this.drawingContext.lineTo(this.lastPoint.x, this.lastPoint.y);
                this.drawingContext.stroke();
            }
            // Save the completed stroke
            if (this.currentStroke.length > 0) {
                this.allStrokes.push([...this.currentStroke]);
            }
            this.isDrawing = false;
            this.currentStroke = [];
            this.lastPoint = null;
            // Release pointer capture
            if (this.canvasElement) {
                this.canvasElement.releasePointerCapture(e.pointerId);
            }
        };
        this.handlePointerCancel = (e) => {
            // Handle cases where drawing is interrupted (e.g., phone call, notification)
            this.isDrawing = false;
            this.currentStroke = [];
            this.lastPoint = null;
            if (this.canvasElement) {
                this.canvasElement.releasePointerCapture(e.pointerId);
            }
        };
        this.handleSelectFile = (e) => {
            var _a;
            e.stopPropagation();
            e.preventDefault();
            (_a = this.fileInputElement) === null || _a === void 0 ? void 0 : _a.click();
        };
        this.handleFileChange = (e) => {
            var _a;
            const input = e.target;
            const file = (_a = input.files) === null || _a === void 0 ? void 0 : _a[0];
            if (!file) {
                return;
            }
            // Validate file type
            if (!file.type.startsWith('image/png')) {
                alert('Please select a PNG image file.');
                input.value = '';
                return;
            }
            // Load and display the image
            const reader = new FileReader();
            reader.onload = (event) => {
                var _a;
                const img = new Image();
                img.onload = () => {
                    this.uploadedImage = img;
                    this.uploadedFileName = file.name;
                    this.hasUploadedImage = true;
                    this.drawUploadedImage();
                };
                img.onerror = () => {
                    alert('Failed to load image. Please try a different file.');
                    input.value = '';
                };
                img.src = (_a = event.target) === null || _a === void 0 ? void 0 : _a.result;
            };
            reader.onerror = () => {
                alert('Failed to read file. Please try again.');
                input.value = '';
            };
            reader.readAsDataURL(file);
        };
        this.handleClearUpload = (e) => {
            e.stopPropagation();
            e.preventDefault();
            this.uploadedImage = null;
            this.uploadedFileName = '';
            this.hasUploadedImage = false;
            // Clear the file input
            if (this.fileInputElement) {
                this.fileInputElement.value = '';
            }
            // Clear the canvas
            if (this.canvasElement) {
                const ctx = this.canvasElement.getContext('2d');
                if (ctx) {
                    ctx.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height);
                }
            }
        };
    }
    componentWillLoad() {
        this.enteredName = this.name;
        const ds = new FontFace('Dancing Script', 'url(https://fonts.gstatic.com/s/dancingscript/v19/If2cXTr6YS-zF4S-kcSWSVi_sxjsohD9F50Ruu7BMSo3Sup6hNX6plRP.woff)');
        ds.load().then(font => {
            document.fonts.add(font);
            this.fontLoaded = true;
        });
    }
    componentDidLoad() {
        this.redrawSignature();
        this.setupDrawingCanvas();
    }
    componentDidUpdate() {
        this.redrawSignature();
        this.setupDrawingCanvas();
        // Redraw uploaded image when in upload mode
        if (this.mode === 'upload' && this.hasUploadedImage) {
            this.drawUploadedImage();
        }
        // Redraw drawn signature when in draw mode
        if (this.mode === 'draw' && this.allStrokes.length > 0) {
            this.redrawDrawnSignature();
        }
    }
    redrawSignature() {
        if (!this.canvasElement || this.mode !== 'type') {
            return;
        }
        const canvasWidth = this.canvasElement.width;
        const context = this.canvasElement.getContext('2d');
        context.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height);
        let fontSize = 100;
        do {
            fontSize -= 2;
            context.font = `${fontSize}px Dancing Script`;
        } while (context.measureText(this.enteredName).width > canvasWidth - 32); // 32px padding each side
        context.textAlign = 'center';
        context.textBaseline = 'middle';
        context.font = `${fontSize}px Dancing Script`;
        context.fillText(this.enteredName, this.canvasElement.width / 2, this.canvasElement.height / 2);
    }
    redrawDrawnSignature() {
        if (!this.canvasElement || !this.drawingContext || this.allStrokes.length === 0) {
            return;
        }
        // Clear the canvas first
        this.drawingContext.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height);
        // Redraw all strokes
        for (const stroke of this.allStrokes) {
            if (stroke.length === 0) {
                continue;
            }
            // Start the path at the first point
            this.drawingContext.beginPath();
            this.drawingContext.moveTo(stroke[0].x, stroke[0].y);
            // Draw smooth curves through all points in the stroke
            for (let i = 1; i < stroke.length; i++) {
                const currentPoint = stroke[i];
                const previousPoint = stroke[i - 1];
                // Use quadratic curves for smooth lines
                const midPoint = {
                    x: (previousPoint.x + currentPoint.x) / 2,
                    y: (previousPoint.y + currentPoint.y) / 2,
                };
                this.drawingContext.quadraticCurveTo(previousPoint.x, previousPoint.y, midPoint.x, midPoint.y);
            }
            // Draw to the last point
            if (stroke.length > 1) {
                const lastPoint = stroke[stroke.length - 1];
                this.drawingContext.lineTo(lastPoint.x, lastPoint.y);
            }
            this.drawingContext.stroke();
        }
    }
    handleNameChange(e) {
        this.enteredName = e.target.value;
    }
    handleCancel(e) {
        e.stopPropagation();
        e.preventDefault();
        this.exit.emit();
    }
    handleAdopt(e) {
        e.stopPropagation();
        e.preventDefault();
        const data = this.canvasElement.toDataURL('image/png');
        this.next.emit(data);
    }
    isAdoptButtonDisabled() {
        switch (this.mode) {
            case 'type':
                // Disable if no name has been entered
                return !this.enteredName || this.enteredName.trim().length === 0;
            case 'draw':
                // Disable if nothing has been drawn
                return !this.hasDrawnSignature || this.allStrokes.length === 0;
            case 'upload':
                // Disable if no file has been uploaded
                return !this.hasUploadedImage;
            default:
                return true;
        }
    }
    setupDrawingCanvas() {
        if (!this.canvasElement) {
            return;
        }
        // Set up drawing context when in draw mode
        if (this.mode === 'draw') {
            // Always get fresh context from the current canvas element
            const ctx = this.canvasElement.getContext('2d');
            // Only initialize if we don't have a context or it's different
            if (ctx && ctx !== this.drawingContext) {
                this.drawingContext = ctx;
                // Configure drawing style for smooth signatures
                this.drawingContext.strokeStyle = '#000000';
                this.drawingContext.lineWidth = 2;
                this.drawingContext.lineCap = 'round';
                this.drawingContext.lineJoin = 'round';
                // Clear canvas only if there are no existing strokes
                if (this.allStrokes.length === 0) {
                    this.drawingContext.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height);
                }
            }
        }
        // Reset drawing context when switching away from draw mode
        // NOTE: We don't clear allStrokes here so drawing persists when returning to draw mode
        if (this.mode !== 'draw' && this.drawingContext) {
            this.drawingContext = null;
            this.currentStroke = [];
            this.lastPoint = null;
        }
    }
    clearDrawingCanvas() {
        if (!this.canvasElement || !this.drawingContext) {
            return;
        }
        this.drawingContext.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height);
        this.currentStroke = [];
        this.allStrokes = [];
        this.lastPoint = null;
        this.hasDrawnSignature = false;
    }
    handleClearDrawing(e) {
        e.stopPropagation();
        e.preventDefault();
        this.clearDrawingCanvas();
    }
    getCanvasCoordinates(e) {
        if (!this.canvasElement) {
            return { x: 0, y: 0 };
        }
        const rect = this.canvasElement.getBoundingClientRect();
        // Calculate the scale factor between canvas size and display size
        const scaleX = this.canvasElement.width / rect.width;
        const scaleY = this.canvasElement.height / rect.height;
        return {
            x: (e.clientX - rect.left) * scaleX,
            y: (e.clientY - rect.top) * scaleY,
        };
    }
    drawUploadedImage() {
        if (!this.canvasElement || !this.uploadedImage) {
            return;
        }
        const ctx = this.canvasElement.getContext('2d');
        if (!ctx) {
            return;
        }
        // Clear the canvas
        ctx.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height);
        // Calculate scaling to fit image within canvas while maintaining aspect ratio
        const canvasWidth = this.canvasElement.width;
        const canvasHeight = this.canvasElement.height;
        const imageWidth = this.uploadedImage.width;
        const imageHeight = this.uploadedImage.height;
        // Calculate scale to fit within canvas (with some padding)
        const padding = 10;
        const availableWidth = canvasWidth - padding * 2;
        const availableHeight = canvasHeight - padding * 2;
        const scaleX = availableWidth / imageWidth;
        const scaleY = availableHeight / imageHeight;
        const scale = Math.min(scaleX, scaleY);
        // Calculate dimensions and position to center the image
        const scaledWidth = imageWidth * scale;
        const scaledHeight = imageHeight * scale;
        const x = (canvasWidth - scaledWidth) / 2;
        const y = (canvasHeight - scaledHeight) / 2;
        // Draw the image centered and scaled
        ctx.drawImage(this.uploadedImage, x, y, scaledWidth, scaledHeight);
    }
    render() {
        return (h("verdocs-dialog", { key: '0b429c1bfa33dcb68c723632c9d41e804c1c9d4b' }, h("div", { key: '25757ea5b5681d01e8ec795fca09c76f58765892', slot: "heading", class: "heading" }, h("div", { key: '420d55c208a4a459de8d4f13f87549210425cadd', class: "icon" }, h("svg", { key: '81dc9fc0cafbbdd3a528b8d3d30a3bbd4d1e60ac', width: "22", height: "19", viewBox: "0 0 22 19", fill: "none", xmlns: "http://www.w3.org/2000/svg" }, h("g", { key: 'e7ad29bdf97453171ac57e02512196b96f560a97', "clip-path": "url(#clip0_34208_4881)" }, h("path", { key: '3ffd7f641d18d97318c601f58e1b07593c8b9253', d: "M3.125 0.75C1.88398 0.75 0.875 1.75898 0.875 3V16.5C0.875 17.741 1.88398 18.75 3.125 18.75H12.125C13.366 18.75 14.375 17.741 14.375 16.5V15.8215C14.2801 15.8602 14.1852 15.8918 14.0867 15.9164L11.9738 16.4437C11.8684 16.4684 11.7629 16.4859 11.6574 16.493C11.6258 16.4965 11.5941 16.5 11.5625 16.5H9.3125C9.09805 16.5 8.90469 16.3805 8.80977 16.1906L8.50039 15.5684C8.44062 15.4488 8.32109 15.375 8.19102 15.375C8.06094 15.375 7.93789 15.4488 7.88164 15.5684L7.57227 16.1906C7.47031 16.398 7.24883 16.5211 7.02031 16.5C6.7918 16.4789 6.59492 16.3207 6.53164 16.1027L5.9375 14.1445L5.59297 15.2977C5.37852 16.0113 4.72109 16.5 3.97578 16.5H3.6875C3.37812 16.5 3.125 16.2469 3.125 15.9375C3.125 15.6281 3.37812 15.375 3.6875 15.375H3.97578C4.22539 15.375 4.44336 15.2133 4.51367 14.9742L5.0375 13.234C5.15703 12.8367 5.52266 12.5625 5.9375 12.5625C6.35234 12.5625 6.71797 12.8367 6.8375 13.234L7.24531 14.591C7.50547 14.373 7.83594 14.25 8.1875 14.25C8.74648 14.25 9.25625 14.5664 9.50586 15.0656L9.66055 15.375H9.97344C9.86445 15.0656 9.84336 14.7281 9.92422 14.3977L10.4516 12.2848C10.55 11.8875 10.7539 11.5289 11.0422 11.2406L14.375 7.90781V6.375H9.875C9.25273 6.375 8.75 5.87227 8.75 5.25V0.75H3.125ZM9.875 0.75V5.25H14.375L9.875 0.75ZM20.2039 5.66133C19.6555 5.11289 18.766 5.11289 18.2141 5.66133L17.1805 6.69492L19.6766 9.19102L20.7102 8.15742C21.2586 7.60898 21.2586 6.71953 20.7102 6.16758L20.2039 5.66133ZM11.8402 12.0352C11.6961 12.1793 11.5941 12.3586 11.5449 12.559L11.0176 14.6719C10.9684 14.8652 11.0246 15.0656 11.1652 15.2063C11.3059 15.3469 11.5062 15.4031 11.6996 15.3539L13.8125 14.8266C14.0094 14.7773 14.1922 14.6754 14.3363 14.5312L18.8785 9.98555L16.3824 7.48945L11.8402 12.0352Z", fill: "white" })), h("defs", { key: '40470a753022575342a62e03f355259ad2236dfd' }, h("clipPath", { key: 'fd8e87dfd3a0600ea8b00bcdea231c443db263e5', id: "clip0_34208_4881" }, h("path", { key: 'd37762121ab1516b637e23e68123181861f3430b', d: "M0.875 0.75H21.125V18.75H0.875V0.75Z", fill: "white" }))))), h("div", { key: '8aa8a867add1f97217926df053936afe94b72ffb', class: "title" }, "Adopt Signature")), h("div", { key: '7c7ce2c4827272f4b6c42c103f78d79a9aab834a', slot: "content", class: "content" }, h("div", { key: 'f2f7ce37214d1f739f9a2444e65038ee2425dc01', class: "tabs" }, h("div", { key: 'ae94a1a76c04ffda86c9a300c0cb4ae2f967929c', class: { tab: true, active: this.mode === 'type' }, onClick: () => (this.mode = 'type') }, h("div", { key: '50ae13bf5f04376c4e5842dc9752c30f383c213d', innerHTML: Keyboard, class: "icon" }), h("div", { key: 'b7a2dedbead1f59695106a73c4683f40369dadc6' }, "Type")), h("div", { key: '253b9d4506f901f8466eb593b80e99cbec7726c8', class: { tab: true, active: this.mode === 'draw' }, onClick: () => (this.mode = 'draw') }, h("div", { key: 'd9f5dbe36f7fa6052c7a3edad8ff471cb0738879', innerHTML: Pencil, class: "icon" }), h("div", { key: '72e1ca881be3af547e0673733d39e771f9547bf4' }, "Draw")), h("div", { key: '7d280ad986b9203c1c824942f620c7ac673a7974', class: { tab: true, active: this.mode === 'upload' }, onClick: () => (this.mode = 'upload') }, h("div", { key: '673faa7a093acc843b4e67fb707c222ed22a7f33', innerHTML: Upload, class: "icon" }), h("div", { key: '3b5019ec93318c864a16712a4819ea528f1d6094' }, "Upload"))), h("div", { key: '714cc6316f22bf01eeddf50cd1140333dde7240f', class: { type: true, active: this.mode === 'type' } }, h("verdocs-text-input", { key: 'c986c98996c53f950b4b68369c19b0c897d13369', label: "Full Name", value: this.enteredName, placeholder: "Full Name...", description: "As shown on driver's license or govt. ID card.", onInput: e => this.handleNameChange(e), onClick: e => e.stopPropagation() })), h("div", { key: '78c8bd98c799038d87a99c749ca95641a501f875', class: { draw: true, active: this.mode === 'draw' } }, h("div", { key: '8542a85b7bb331c3e2df482ac49c5442e24deeaa', class: "draw-instructions" }, "Draw your signature below using your mouse or finger"), h("div", { key: '3950ea157e7be7a82cee715eb7abff1a44c5e680', class: "draw-actions" }, h("button", { key: 'c0824acb635bbd01bd6084b1b8c5421834a3b9e8', class: "clear-button", onClick: e => this.handleClearDrawing(e), disabled: !this.hasDrawnSignature }, "Clear"))), h("div", { key: 'da28e645bf706d21669062b1b348d2f4c65db574', class: { upload: true, active: this.mode === 'upload' } }, h("div", { key: '33432f5f771a961cbd04a1a459ab3e1c436a3796', class: "upload-instructions" }, "Upload a PNG image of your signature"), h("input", { key: '5272fc359e5ea2c9d08444a6dee7939af0c85ee9', ref: el => (this.fileInputElement = el), type: "file", accept: "image/png", onChange: this.handleFileChange, style: { display: 'none' } }), h("div", { key: 'c8a861ccac56394d5d2b412b261a0fa7abc08c6f', class: "upload-actions" }, h("button", { key: '20130f1880990ead83064dc586cc98d1303e9f82', class: "upload-button", onClick: this.handleSelectFile }, this.hasUploadedImage ? 'Change Image' : 'Select PNG File'), this.hasUploadedImage && (h("button", { key: '441f205d01eadc47add35a2fe07bfd550c47aaeb', class: "clear-button", onClick: this.handleClearUpload }, "Clear"))), this.uploadedFileName && h("div", { key: '3d724075c22a178c440db88464cc7c1197ce74a3', class: "upload-filename" }, this.uploadedFileName)), h("canvas", { key: 'f9bbe470d83ca03666e87455b4c4f73865afff28', ref: el => (this.canvasElement = el), width: "300", height: "79", onPointerDown: this.handlePointerDown, onPointerMove: this.handlePointerMove, onPointerUp: this.handlePointerUp, onPointerCancel: this.handlePointerCancel, style: { touchAction: 'none', cursor: 'crosshair' } }), h("div", { key: '8e309f8f6b19b76e892c9320172e0f2a8db10e9c', class: "disclaimer" }, "By clicking Adopt, I agree that the signature will be the electronic representation of my signature for all purposes when I (or my agent) use them on documents, including legally binding contracts \u2014 just the same as a pen-and-paper signature or initial.")), h("div", { key: '107dd5abf82744aadd7dc551b648488d444eb33c', class: "footer", slot: "footer" }, h("div", { key: 'f10d17289c2884bd8602d4637ce60d8345310c82', class: "buttons" }, h("button", { key: '761f72f1a011c5dbf2d3a8f0491ddae4b6f5b924', class: "cancel", onClick: e => this.handleCancel(e) }, "Cancel"), h("button", { key: '8f93fa813735bda38eef39694986501e7a70067a', class: "proceed", onClick: e => this.handleAdopt(e), disabled: this.isAdoptButtonDisabled() }, "Adopt & Sign")))));
    }
    static get is() { return "verdocs-signature-dialog"; }
    static get originalStyleUrls() {
        return {
            "$": ["verdocs-signature-dialog.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["verdocs-signature-dialog.css"]
        };
    }
    static get properties() {
        return {
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Initial signature text"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "name",
                "defaultValue": "''"
            }
        };
    }
    static get states() {
        return {
            "fontLoaded": {},
            "enteredName": {},
            "mode": {},
            "isDrawing": {},
            "hasDrawnSignature": {},
            "hasUploadedImage": {},
            "uploadedFileName": {}
        };
    }
    static get events() {
        return [{
                "method": "next",
                "name": "next",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Fired when the user completes the dialog and clicks Adopt. The event detail will contain a base64-encoded string\nrepresentation of the signature adopted."
                },
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                }
            }, {
                "method": "exit",
                "name": "exit",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event fired when the step is cancelled. This is called exit to avoid conflicts with the JS-reserved \"cancel\" event name."
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }];
    }
}
//# sourceMappingURL=verdocs-signature-dialog.js.map
