import { defineComponent, ref, watch, computed, onMounted, openBlock, createBlock, Transition, withCtx, withDirectives, createElementVNode, normalizeClass, unref, normalizeStyle, createElementBlock, toDisplayString, createCommentVNode, vShow } from "vue";
import "../../../adapter-vue.mjs";
import { isH5 } from "../../../utils/env.mjs";
import TOAST_TYPE from "./type.mjs";
const _hoisted_1 = { key: 0 };
const _hoisted_2 = { key: 1 };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    message: {
      type: String,
      default: ""
    },
    duration: {
      type: Number,
      default: 3e3
    },
    repeatNum: {
      type: Number,
      default: 1
    },
    id: {
      type: String,
      default: ""
    },
    onClose: {
      type: Function,
      required: false
    },
    offset: {
      type: Number,
      default: 20
    },
    zIndex: {
      type: Number,
      default: 0
    },
    type: {
      type: String,
      default: ""
    }
  },
  setup(__props) {
    const props = __props;
    const visible = ref(false);
    let timer;
    const startTimer = () => {
      if (props.duration > 0) {
        timer = setTimeout(() => {
          if (visible.value) {
            close();
          }
        }, props.duration);
      }
    };
    const clearTimer = () => {
      clearTimeout(timer);
    };
    const close = () => {
      visible.value = false;
      if (typeof props.onClose === "function") {
        props.onClose();
      }
    };
    watch(
      () => props.repeatNum,
      () => {
        clearTimer();
        startTimer();
      }
    );
    const customStyle = computed(() => ({
      top: `${props.offset}px`,
      zIndex: props.zIndex
    }));
    onMounted(() => {
      startTimer();
      visible.value = true;
    });
    const handleStyle = (type) => {
      if (type && (type === TOAST_TYPE.ERROR || type === TOAST_TYPE.SUCCESS || type === TOAST_TYPE.WARNING))
        return type;
      return TOAST_TYPE.NORMAL;
    };
    return (_ctx, _cache) => {
      return openBlock(), createBlock(Transition, {
        name: "fade",
        onAfterLeave: _cache[0] || (_cache[0] = ($event) => _ctx.$emit("destroy"))
      }, {
        default: withCtx(() => [
          withDirectives(createElementVNode("div", {
            class: normalizeClass(["message", [handleStyle(__props.type), unref(isH5) && "message-h5"]]),
            style: normalizeStyle(unref(customStyle))
          }, [
            !unref(isH5) ? (openBlock(), createElementBlock("p", _hoisted_1, toDisplayString(__props.message), 1)) : createCommentVNode("", true),
            unref(isH5) ? (openBlock(), createElementBlock("span", _hoisted_2, toDisplayString(__props.message), 1)) : createCommentVNode("", true)
          ], 6), [
            [vShow, unref(visible)]
          ])
        ]),
        _: 1
      });
    };
  }
});
export {
  _sfc_main as default
};
