import { __decorate, __metadata } from "tslib";
import './user-role-editor';
import gql from 'graphql-tag';
import { css, html, LitElement } from 'lit';
import { customElement, query } from 'lit/decorators.js';
import { client, gqlContext } from '@operato/graphql';
import { i18next, localize } from '@operato/i18n';
import { OxPrompt } from '@operato/popup/ox-prompt.js';
let InviteUser = class InviteUser extends localize(i18next)(LitElement) {
    render() {
        return html `
      <input name="email" type="email" required name="invite-email" autocapitalize="off" />
      <mwc-button
        @click=${this.invite.bind(this)}
        outlined
        icon="group_add"
        label=${String(i18next.t('label.invite user'))}
      ></mwc-button>
    `;
    }
    async invite() {
        try {
            if (!this.emailInput.checkValidity()) {
                throw new Error(i18next.t('error.not valid pattern of type', { type: 'e-mail' }));
            }
            if (await OxPrompt.open({
                title: i18next.t('text.are_you_sure'),
                text: i18next.t('text.do_you_want_to_invite_x', { x: i18next.t(`label.user`) }),
                confirmButton: { text: i18next.t('button.confirm') },
                cancelButton: { text: i18next.t('button.cancel') }
            })) {
                await this.inviteUser(this.emailInput.value);
                this.dispatchEvent(new CustomEvent('invitationCompleted'));
            }
        }
        catch (e) {
            document.dispatchEvent(new CustomEvent('notify', {
                detail: {
                    level: 'error',
                    message: 'message' in e ? e.message : e
                }
            }));
        }
    }
    async inviteUser(email) {
        const response = await client.mutate({
            mutation: gql `
        mutation inviteUser($email: EmailAddress!) {
          inviteUser(email: $email)
        }
      `,
            variables: { email },
            context: gqlContext()
        });
        if (!response.errors) {
            await OxPrompt.open({
                title: i18next.t('text.completed'),
                confirmButton: { text: i18next.t('button.confirm') }
            });
            this.emailInput.value = '';
        }
    }
};
InviteUser.styles = css `
    :host {
      display: grid;
    }

    input {
      flex: 1;

      border: var(--border-dark-color);
      border-radius: var(--border-radius);
      margin: var(--input-margin);
      padding: var(--input-padding);
      min-width: 250px;
      font: var(--input-font);
    }

    mwc-button {
      margin: var(--input-margin);
    }
  `;
__decorate([
    query('input[name=email]'),
    __metadata("design:type", HTMLInputElement)
], InviteUser.prototype, "emailInput", void 0);
InviteUser = __decorate([
    customElement('invite-user')
], InviteUser);
//# sourceMappingURL=invite-user.js.map