import { verifySigner } from "@verdocs/js-sdk";
import { h, Host } from "@stencil/core";
import { DefaultEndpoint } from "../../../utils/Environment";
const QuestionIcon = `<svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M20 0C8.95313 0 0 8.95313 0 20C0 31.0469 8.95313 40 20 40C31.0469 40 40 31.0469 40 20C40 8.95313 31.0469 0 20 0ZM20 36.25C11.0391 36.25 3.75 28.9602 3.75 20C3.75 11.0398 11.0391 3.75 20 3.75C28.9609 3.75 36.25 11.0398 36.25 20C36.25 28.9602 28.9609 36.25 20 36.25Z" fill="white"/><path d="M20 26.25C18.5938 26.25 17.5 27.3438 17.5 28.75C17.5 30.1562 18.5234 31.25 20 31.25C21.3359 31.25 22.5 30.1562 22.5 28.75C22.5 27.3438 21.3359 26.25 20 26.25ZM22.5859 10H18.5938C15.5469 10 13.125 12.4219 13.125 15.4688C13.125 16.4844 13.9844 17.3438 15 17.3438C16.0156 17.3438 16.875 16.4844 16.875 15.4688C16.875 14.5312 17.5859 13.75 18.5234 13.75H22.5156C23.5234 13.75 24.375 14.5312 24.375 15.4688C24.375 16.0938 24.0625 16.5703 23.5156 16.8828L19.0625 19.6094C18.4375 20 18.125 20.625 18.125 21.25V22.5C18.125 23.5156 18.9844 24.375 20 24.375C21.0156 24.375 21.875 23.5156 21.875 22.5V22.3438L25.3984 20.1562C27.0391 19.1406 28.0547 17.3438 28.0547 15.4688C28.125 12.4219 25.7031 10 22.5859 10Z" fill="#E7E7E7"/></svg>`;
/**
 * Prompt the user to confirm their identity with a one time code via email/SMS.
 */
export class VerdocsOtpDialog {
    constructor() {
        this.resendTimeout = null;
        /**
         * The endpoint to use to communicate with Verdocs. If not set, the default endpoint will be used.
         */
        this.endpoint = DefaultEndpoint;
        /**
         * The type of dialog to display. Three modes are supported.
         */
        this.method = 'email';
        /**
         * For identity confirmation, the current recipient details.
         */
        this.recipient = null;
        this.resendDisabled = true;
        this.response = '';
    }
    componentDidLoad() {
        this.startResendTimeout();
        // Give the parent time to finish what it's doing.
        setTimeout(() => {
            verifySigner(this.endpoint, { auth_method: this.method, code: '', resend: true })
                .then(r => {
                console.log('Resend result', r);
                this.response = '';
            })
                .catch(e => console.log('Error resending code', e));
        }, 1000);
    }
    disconnectedCallback() {
        if (this.resendTimeout) {
            clearTimeout(this.resendTimeout);
            this.resendTimeout = null;
        }
    }
    handleCancel() {
        this.exit.emit();
    }
    // We need a separate event handler for clicking the background because it can receive events "through" other child components
    handleDismiss(e) {
        if (e.target.class === 'background-overlay') {
            e.preventDefault();
            this.handleCancel();
        }
    }
    startResendTimeout() {
        this.resendDisabled = true;
        if (this.resendTimeout) {
            clearTimeout(this.resendTimeout);
        }
        this.resendTimeout = setTimeout(() => {
            this.resendDisabled = false;
        }, 30000);
    }
    handleResend() {
        this.startResendTimeout();
        verifySigner(this.endpoint, { auth_method: this.method, code: '', resend: true })
            .then(r => {
            console.log('Resend result', r);
            this.response = '';
            window.alert('A new code has been sent.');
        })
            .catch(e => console.log('Error resending code', e));
    }
    handleSubmit() {
        this.startResendTimeout();
        verifySigner(this.endpoint, { auth_method: this.method, code: this.response })
            .then(response => {
            console.log('Verification completed', response);
            this.next.emit({ response });
            window.alert('Thank you. Your verification was successful.');
        })
            .catch(e => {
            window.alert('Invalid verification code. Please try again.');
            console.log('Invalid code', e);
        });
        this.response = '';
    }
    render() {
        return (h(Host, { key: 'b264293071f7bef8b21a8a271cefc361b1a9988c' }, h("div", { key: '1a0e3b861dff92f5e73980237f3c682bf5864e4a', class: "background-overlay", onClick: e => this.handleDismiss(e) }, h("div", { key: '9530b8a2aa6bf5463af6a581e922421db5451a01', class: "dialog" }, h("div", { key: '904c74579299b714e5bfdd3c9640b62829f4fe75', class: "heading" }, "Verification Required"), h("div", { key: '4c47e33939d2683c58fec11bff9f527bff85b456', class: "help-box" }, h("div", { key: '7318779bce06bf3b6ce8a2b5823124444a76072d', class: "help-icon", innerHTML: QuestionIcon }), h("div", { key: 'e9bef6188ac6046720ed2a6388256f2afe3f0176', class: "help-details" }, h("div", { key: '8407632733f4c6284b440dea8ca88f498689c0d9', class: "help-title" }, "Please verify your email address"), h("div", { key: 'ff4098afa88eb804d821056786a47c6dd9237da4', class: "help-text" }, "Check your email for a one-time code, and enter it below."))), h("div", { key: '242fbeef76e8bf1e06b8525670acfd97b93254a7', class: "input" }, h("label", { key: '252fef5d5e6ba5e2dac59d4180acfe58fb8c7c58', htmlFor: "verdocs-otp-input" }, "Code:"), h("input", { key: '85f03f11f6dd47a734cc10a3aac97cd6d4c901ff', type: "text", id: "verdocs-otp-input", name: "verdocs-otp-input", placeholder: "Enter your one-time code...", value: this.response, onInput: (e) => (this.response = e.target.value) })), h("div", { key: '7d9c09905b2c0279350113a937427ec0e14f3cff', class: "buttons" }, h("verdocs-button", { key: 'cdb7f5906897c90c4fe5b0f1aeb2a7b9244136ce', label: "Cancel", variant: "outline", onClick: () => this.handleCancel() }), h("verdocs-button", { key: '679184fd5eac96a8240e67e9b3264076b2a8ae14', label: 'Resend', onClick: () => this.handleResend(), disabled: this.resendDisabled }), h("verdocs-button", { key: '24ec1dbf72f580818dade1f39bdbd5723af403a5', label: 'Submit', onClick: () => this.handleSubmit(), disabled: !this.response }))))));
    }
    static get is() { return "verdocs-otp-dialog"; }
    static get originalStyleUrls() {
        return {
            "$": ["verdocs-otp-dialog.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["verdocs-otp-dialog.css"]
        };
    }
    static get properties() {
        return {
            "endpoint": {
                "type": "unknown",
                "attribute": "endpoint",
                "mutable": true,
                "complexType": {
                    "original": "VerdocsEndpoint",
                    "resolved": "VerdocsEndpoint",
                    "references": {
                        "VerdocsEndpoint": {
                            "location": "import",
                            "path": "@verdocs/js-sdk",
                            "id": "node_modules::VerdocsEndpoint"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The endpoint to use to communicate with Verdocs. If not set, the default endpoint will be used."
                },
                "getter": false,
                "setter": false,
                "defaultValue": "DefaultEndpoint"
            },
            "method": {
                "type": "string",
                "attribute": "method",
                "mutable": false,
                "complexType": {
                    "original": "'email' | 'sms'",
                    "resolved": "\"email\" | \"sms\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The type of dialog to display. Three modes are supported."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "'email'"
            },
            "recipient": {
                "type": "unknown",
                "attribute": "recipient",
                "mutable": false,
                "complexType": {
                    "original": "IRecipient | null",
                    "resolved": "IRecipient",
                    "references": {
                        "IRecipient": {
                            "location": "import",
                            "path": "@verdocs/js-sdk",
                            "id": "node_modules::IRecipient"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "For identity confirmation, the current recipient details."
                },
                "getter": false,
                "setter": false,
                "defaultValue": "null"
            }
        };
    }
    static get states() {
        return {
            "resendDisabled": {},
            "response": {}
        };
    }
    static get events() {
        return [{
                "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": {}
                }
            }, {
                "method": "next",
                "name": "next",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event fired when the process has completed successfully."
                },
                "complexType": {
                    "original": "{response: ISignerTokenResponse}",
                    "resolved": "{ response: ISignerTokenResponse; }",
                    "references": {
                        "ISignerTokenResponse": {
                            "location": "import",
                            "path": "@verdocs/js-sdk",
                            "id": "node_modules::ISignerTokenResponse"
                        }
                    }
                }
            }];
    }
}
//# sourceMappingURL=verdocs-otp-dialog.js.map
