<template>
  <ds-space v-if="!data && !error" margin="large">
    <ds-form
      v-model="formData"
      :schema="formSchema"
      @input="handleInput"
      @input-valid="handleInputValid"
      @submit="handleSubmit"
    >
      <!-- Wolle <h1>
        {{
          invitation
            ? $t('profile.invites.title', metadata)
            : $t('components.registration.signup.title', metadata)
        }}
      </h1> -->
      <ds-text v-if="token" v-html="$t('registration.signup.form.invitation-code', { code: token })" />
      <ds-text>
        {{
          invitation
            ? $t('profile.invites.description')
            : $t('components.registration.signup.form.description')
        }}
      </ds-text>
      <ds-input
        :placeholder="invitation ? $t('profile.invites.emailPlaceholder') : $t('login.email')"
        type="email"
        id="email"
        model="email"
        name="email"
        icon="envelope"
      />
      <!-- <base-button
        :disabled="disabled"
        :loading="$apollo.loading"
        filled
        name="submit"
        type="submit"
        icon="envelope"
      >
        {{ $t('components.registration.signup.form.submit') }}
      </base-button> -->
      <slot></slot>
    </ds-form>
  </ds-space>
  <div v-else margin="large">
    <template v-if="!error">
      <transition name="ds-transition-fade">
        <sweetalert-icon icon="info" />
      </transition>
      <ds-text align="center" v-html="submitMessage" />
    </template>
    <template v-else>
      <transition name="ds-transition-fade">
        <sweetalert-icon icon="error" />
      </transition>
      <ds-text align="center">{{ error.message }}</ds-text>
      <ds-space centered class="space-top">
        <nuxt-link to="/login">{{ $t('site.back-to-login') }}</nuxt-link>
      </ds-space>
    </template>
  </div>
</template>

<script>
import gql from 'graphql-tag'
import metadata from '~/constants/metadata'
import { isEmail } from 'validator'
import normalizeEmail from '~/components/utils/NormalizeEmail'
import { SweetalertIcon } from 'vue-sweetalert-icons'

export const SignupMutation = gql`
  mutation($email: String!) {
    Signup(email: $email) {
      email
    }
  }
`
export const SignupByInvitationMutation = gql`
  mutation($email: String!, $token: String!) {
    SignupByInvitation(email: $email, token: $token) {
      email
    }
  }
`
export default {
  name: 'RegistrationItemEnterEmail',
  components: {
    SweetalertIcon,
  },
  props: {
    sliderData: { type: Object, required: true },
    token: { type: String, default: null }, // Wolle not used???
    invitation: { type: Boolean, default: false },
  },
  data() {
    return {
      metadata,
      formData: {
        email: '',
      },
      formSchema: {
        email: {
          type: 'email',
          required: true,
          message: this.$t('common.validations.email'),
        },
      },
      // disabled: true,
      data: null,
      error: null,
    }
  },
  mounted: function () {
    this.$nextTick(function () {
      // Code that will run only after the entire view has been rendered
      // console.log('mounted !!! ')
      this.formData.email = this.sliderData.collectedInputData.email
        ? this.sliderData.collectedInputData.email
        : ''
      this.sendValidation()
    })
  },
  computed: {
    submitMessage() {
      const { email } = this.data.Signup
      return this.$t('components.registration.signup.form.success', { email })
    },
    valid() {
      let isValid
      if (isEmail(this.formData.email)) {
        this.formData.email = normalizeEmail(this.formData.email)
        isValid = true
      } else {
        isValid = false
      }
      return isValid
    },
  },
  methods: {
    sendValidation() {
      const { email } = this.formData
      const value = {
        email,
      }
      // console.log('sendValidation !!! value: ', value)
      this.sliderData.validateCallback(this.valid, value)
    },
    handleInput() {
      // this.disabled = true
      // this.sliderData.validateCallback(false)
      this.sendValidation()
    },
    handleInputValid() {
      // this.disabemailled = false
      // const { email } = this.formData
      // validate in backend?
      // toaster?
      // console.log('sendValidation !!! email: ', email)
      // this.sliderData.validateCallback(true, { email })
      this.sendValidation()
    },
    async handleSubmit() {
      const mutation = this.token ? SignupByInvitationMutation : SignupMutation
      const { token } = this
      const { email } = this.formData

      try {
        const response = await this.$apollo.mutate({ mutation, variables: { email, token } })
        this.data = response.data
        setTimeout(() => {
          this.$emit('submit', { email: this.data.Signup.email })
        }, 3000)
      } catch (err) {
        const { message } = err
        const mapping = {
          'A user account with this email already exists': 'email-exists',
          'Invitation code already used or does not exist': 'invalid-invitation-token',
        }
        for (const [pattern, key] of Object.entries(mapping)) {
          if (message.includes(pattern))
            this.error = {
              key,
              message: this.$t(`components.registration.signup.form.errors.${key}`),
            }
        }
        if (!this.error) {
          this.$toast.error(message)
        }
      }
    },
  },
}
</script>
<style>
.space-top {
  margin-top: 6ex;
}
</style>
