<template>
  <Expandable toggle-on-click>
    <template #header="{ expanded }">
      <div class="event__header">
        <div class="event__header-left">
          <div class="event__label">
            {{ event.label }}
          </div>
          <div
            v-if="workflowActions.length"
            class="margin-left-1 event__amount-actions"
          >
            {{ workflowActions.length }}
          </div>
        </div>
        <a class="event__toggle">
          <i :class="getIcon(expanded)"></i>
        </a>
      </div>
    </template>
    <template #default>
      <WorkflowAction
        v-for="workflowAction in workflowActions"
        :key="workflowAction.id"
        class="margin-top-2 event__workflow-action"
        :available-workflow-action-types="availableWorkflowActionTypes"
        :workflow-action="workflowAction"
        @delete="deleteWorkflowAction(workflowAction)"
        @update="updateWorkflowAction(workflowAction, $event)"
      />
      <Button
        size="tiny"
        type="link"
        prepend-icon="baserow-icon-plus"
        :loading="addingAction"
        class="margin-top-1"
        @click="addWorkflowAction"
      >
        {{ $t('event.addAction') }}
      </Button>
    </template>
  </Expandable>
</template>

<script>
import { Event } from '@baserow/modules/builder/eventTypes'
import WorkflowAction from '@baserow/modules/core/components/workflowActions/WorkflowAction.vue'
import { NotificationWorkflowActionType } from '@baserow/modules/builder/workflowActionTypes'
import { mapActions } from 'vuex'
import { notifyIf } from '@baserow/modules/core/utils/error'

const DEFAULT_WORKFLOW_ACTION_TYPE = NotificationWorkflowActionType.getType()

export default {
  name: 'Event',
  components: { WorkflowAction },
  inject: ['page'],
  props: {
    event: {
      type: Event,
      required: true,
    },
    element: {
      type: Object,
      required: true,
    },
    workflowActions: {
      type: Array,
      required: false,
      default: () => [],
    },
    availableWorkflowActionTypes: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      addingAction: false,
    }
  },
  methods: {
    ...mapActions({
      actionCreateWorkflowAction: 'workflowAction/create',
      actionDeleteWorkflowAction: 'workflowAction/delete',
    }),
    getIcon(expanded) {
      return expanded ? 'iconoir-nav-arrow-down' : 'iconoir-nav-arrow-right'
    },
    async addWorkflowAction() {
      this.addingAction = true
      try {
        await this.actionCreateWorkflowAction({
          page: this.page,
          workflowActionType: DEFAULT_WORKFLOW_ACTION_TYPE,
          eventType: this.event.getType(),
          configuration: {
            element_id: this.element.id,
          },
        })
      } catch (error) {
        notifyIf(error)
      }
      this.addingAction = false
    },
    async deleteWorkflowAction(workflowAction) {
      try {
        await this.actionDeleteWorkflowAction({
          page: this.page,
          workflowAction,
        })
      } catch (error) {
        notifyIf(error)
      }
    },
  },
}
</script>