<template>
  <div
    v-if="show"
    :class="[wrapperClass, alertClasses, 'n-flex n-gap-6 n-align-center']"
  >
    <div class="n-alert-icon-wrapper">
      <n-svg :name="`alert-${type}`" />
    </div>

    <div class="n-alert-content-wrapper n-flex n-gap-6 n-flex-grow">
      <div
        class="n-alert-content n-flex-grow n-flex n-flex-wrap n-flex-column n-gap-3"
      >
        <slot name="titleContent">
          <h6 v-if="title" class="n-alert-content-title n-text-h6 n-font-bold">
            {{ title }}
          </h6>
        </slot>

        <slot name="descriptionContent">
          <p
            v-if="description"
            class="n-alert-content-description n-mb-0 n-text-body-2 n-font-normal n-color-main-font"
          >
            {{ description }}
          </p>
        </slot>
      </div>

      <slot v-if="dismissible" name="dismiss" :onDismiss="onDismiss">
        <button @click="onDismiss">
          <n-icon name="n-icon-add n-alert-cancel-icon" size="32" />
        </button>
      </slot>
    </div>
  </div>
</template>

<script>
import { defineComponent } from "vue";
import NIcon from "../NIcon/n-icon.vue";
import NSvg from "../Svgs/nSvg.vue";

export default defineComponent({
  name: "NAlert",
  components: {
    NSvg,
    NIcon,
  },
  props: {
    value: {
      type: Boolean,
      default: true,
    },
    wrapperClass: {
      type: String,
      default: "",
    },
    description: {
      type: String,
      default: "",
    },
    title: {
      type: String,
      default: "",
    },
    dismissible: {
      type: Boolean,
      default: true,
    },
    size: {
      type: String,
      default: "sm",
      validator(value) {
        return ["sm", "default"].includes(value);
      },
    },
    type: {
      type: String,
      default: "success",
      validator(value) {
        return ["success", "info", "warning", "danger"].includes(value);
      },
    },
  },
  data() {
    return {
      show: true,
    };
  },
  computed: {
    alertClasses() {
      const { size, type } = this;
      const classes = `pkg-base-n-alert n-alert n-alert-size--${size} n-alert--${type} n-radius-md n-px-4`;

      return classes;
    },
  },
  mounted() {
    this.show = this.value;
  },
  methods: {
    onDismiss() {
      this.show = false;
      this.$emit("input", this.show);
    },
  },
});
</script>

<style scoped>
.pkg-base-n-alert.n-alert-size--default {
  padding-top: var(--layout-space-4);
  padding-bottom: var(--layout-space-4);
}

.pkg-base-n-alert.n-alert-size--sm {
  padding-top: var(--layout-space-2);
  padding-bottom: var(--layout-space-2);
}

.pkg-base-n-alert .n-alert-cancel-icon {
  transform: rotate(45deg);
}

.pkg-base-n-alert .n-alert-icon-wrapper svg {
  width: 100%;
  height: 100%;
}

.pkg-base-n-alert.n-alert-size--default .n-alert-icon-wrapper {
  width: 50px;
  height: 50px;
}

.pkg-base-n-alert.n-alert-size--sm .n-alert-icon-wrapper {
  width: 30px;
  height: 30px;
}
</style>
