<template>
  <view :class="tabClass" :style="tabStyle" :id="tabId" @click="onClick">
    <slot>
      {{ title }}
    </slot>
  </view>
</template>

<script lang="ts">
import { defineComponent as _defineComponent } from "vue";
import {
  computed,
  inject,
  onMounted,
  onUnmounted,
  getCurrentInstance,
  nextTick
} from "vue";
import { classNames, stringifyStyle, createBem, uniqid } from "../utils";
import { tabContextSymbol, tabProps } from "../tabs/common";
import { getBoundingClientRect } from "../utils";
const __default__ = {
  options: {
    virtualHost: true,
    styleIsolation: "shared"
  }
};
export default /* @__PURE__ */ _defineComponent({
  ...__default__,
  __name: "tab",
  props: tabProps,
  emits: ["click"],
  setup(__props, { expose: __expose, emit: __emit }) {
    __expose();
    const props = __props;
    const emit = __emit;
    const bem = createBem("tabs");
    const context = inject(tabContextSymbol);
    if (!context) {
      throw new Error("Tab must be included in Tabs.");
    }
    const instance = getCurrentInstance();
    const isCurrent = computed(() => {
      return context.current === props.name;
    });
    const tabId = uniqid();
    const getRect = () => {
      return getBoundingClientRect(`#${tabId}`, instance);
    };
    const select = () => {
      context.select(props.name);
    };
    const onClick = (event) => {
      emit("click", event);
      if (!props.disabled) {
        select();
      }
    };
    onMounted(() => {
      context.register(props.name, {
        getRect
      });
      if (isCurrent.value) {
        nextTick(() => {
          select();
        });
      }
    });
    onUnmounted(() => {
      context.unregister(props.name);
    });
    const tabClass = computed(() => {
      return classNames(
        bem.e("tab"),
        bem.em("tab", "current", isCurrent.value),
        bem.em("tab", "disabled", props.disabled),
        props.rootClass
      );
    });
    const tabStyle = computed(() => {
      return stringifyStyle(props.rootStyle);
    });
    const __returned__ = { props, emit, bem, context, instance, isCurrent, tabId, getRect, select, onClick, tabClass, tabStyle };
    return __returned__;
  }
});
</script>

<style lang="scss">
@use './index.scss';
</style>