import { verifySigner } from "@verdocs/js-sdk";
import { h, Host } from "@stencil/core";
import { DefaultEndpoint } from "../../../utils/Environment";
import { VerdocsToast } from "../../../utils/Toast";
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 = '';
            VerdocsToast('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 });
            VerdocsToast('Thank you. Your verification was successful.');
        })
            .catch(e => {
            VerdocsToast('Invalid verification code. Please try again.');
            console.log('Invalid code', e);
        });
        this.response = '';
    }
    render() {
        return (h(Host, { key: '09de0f0a2b9ea033015df659dc895294191eec1f' }, h("div", { key: '9dcd8ec1a69112f987c65bf005343d0aa1e92762', class: "background-overlay", onClick: e => this.handleDismiss(e) }, h("div", { key: '9439753f7c33b3d3316ce2be193beb6e942486c3', class: "dialog" }, h("div", { key: 'e4c9d1623ae43ecb73b0beba8476b8814a77bf21', class: "heading" }, "Verification Required"), h("div", { key: '4d0db169d6dc13442211d51ff25c2dbacb06e324', class: "help-box" }, h("div", { key: '680a7555654c0fb0fef0ec92a52f9f855c59ed30', class: "help-icon", innerHTML: QuestionIcon }), h("div", { key: 'b4017fcac2057b479be299a460e9fbf830554998', class: "help-details" }, h("div", { key: 'a92c2987721de5b2ccd47f9d6ed207b9c47f3815', class: "help-title" }, "Please verify your email address"), h("div", { key: 'a515d46a0be39a874d45a454d85933f8b87e6e5e', class: "help-text" }, "Check your email for a one-time code, and enter it below."))), h("div", { key: 'aeb05ff5cf9ffb11581ebbebbc79b7d4f2180d76', class: "input" }, h("label", { key: 'a120deeba33e465da4af63f557ec9804e5e3c4ef', htmlFor: "verdocs-otp-input" }, "Code:"), h("input", { key: '3a54ac0a2d41bcf20c7fed828ae7d923e6eb3f2d', 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: '30c9d9f6a9c0b0443246d44ac89e087616b6ebf8', class: "buttons" }, h("verdocs-button", { key: '4082e2410098b2c33f56375d5bef50acfc8db188', label: "Cancel", variant: "outline", onClick: () => this.handleCancel() }), h("verdocs-button", { key: '7d3fd049f06ba8101375d5acfe58feb74622dca3', label: 'Resend', onClick: () => this.handleResend(), disabled: this.resendDisabled }), h("verdocs-button", { key: 'da8b815441904ce1e97d9a3c1cc18d91cf13f92d', 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
