import { toRefs, computed, defineComponent, shallowRef, reactive, inject, onUpdated, onBeforeMount, onMounted, watch, createVNode, resolveComponent, provide, mergeProps, onUnmounted } from "vue";
const tabsProps = {
  modelValue: {
    type: [String, Number],
    default: null
  },
  type: {
    type: String,
    default: "tabs"
  },
  showContent: {
    type: Boolean,
    default: true
  },
  reactivable: {
    type: Boolean,
    default: true
  },
  customWidth: {
    type: String,
    default: ""
  },
  cssClass: {
    type: String,
    default: ""
  },
  beforeChange: {
    type: Function,
    default: null
  },
  closeable: {
    type: Boolean,
    default: false
  },
  addable: {
    type: Boolean,
    default: false
  },
  tabPosition: {
    type: String,
    default: "top"
  }
};
var tabs = "";
function createBem(namespace, element, modifier) {
  let cls = namespace;
  if (element) {
    cls += `__${element}`;
  }
  if (modifier) {
    cls += `--${modifier}`;
  }
  return cls;
}
function useNamespace(block, needDot = false) {
  const namespace = needDot ? `.devui-${block}` : `devui-${block}`;
  const b = () => createBem(namespace);
  const e = (element) => element ? createBem(namespace, element) : "";
  const m = (modifier) => modifier ? createBem(namespace, "", modifier) : "";
  const em = (element, modifier) => element && modifier ? createBem(namespace, element, modifier) : "";
  return {
    b,
    e,
    m,
    em
  };
}
const ns$1 = useNamespace("tabs");
function useTabNavRender(props, data) {
  const { cssClass, tabPosition, customWidth } = toRefs(props);
  const ulClasses = computed(() => ({
    [ns$1.e("nav")]: true,
    [ns$1.em("nav", props.type)]: true,
    [cssClass.value]: Boolean(cssClass.value),
    [ns$1.em("nav", "top")]: tabPosition.value === "top",
    [ns$1.em("nav", "right")]: tabPosition.value === "right",
    [ns$1.em("nav", "bottom")]: tabPosition.value === "bottom",
    [ns$1.em("nav", "left")]: tabPosition.value === "left"
  }));
  const aClasses = computed(() => ({
    ["custom-width"]: Boolean(customWidth.value)
  }));
  const customStyle = {
    width: props.customWidth ? props.customWidth : ""
  };
  const sliderAnimationStyle = computed(() => {
    if (["top", "bottom"].includes(props.tabPosition)) {
      return {
        left: data.offsetLeft + "px",
        width: data.offsetWidth + "px"
      };
    } else {
      return {
        top: data.offsetTop + "px",
        height: data.offsetHeight + "px",
        width: data.offsetWidth + "px"
      };
    }
  });
  return { ulClasses, aClasses, customStyle, sliderAnimationStyle };
}
function useTabNavFunction(props, tabs2, data, ctx, tabsEle) {
  const update = () => {
    if (props.type === "slider") {
      setTimeout(() => {
        var _a;
        const tabEle = (_a = tabsEle.value) == null ? void 0 : _a.querySelector("#" + props.modelValue + ".active");
        if (tabEle && tabsEle.value) {
          if (["top", "bottom"].includes(props.tabPosition)) {
            data.offsetLeft = tabEle.getBoundingClientRect().left - tabsEle.value.getBoundingClientRect().left;
          } else {
            data.offsetTop = tabEle.getBoundingClientRect().top - tabsEle.value.getBoundingClientRect().top;
            data.offsetHeight = tabEle.getBoundingClientRect().height;
          }
          data.offsetWidth = tabEle.getBoundingClientRect().width;
        }
      });
    }
  };
  const canChange = (currentTab) => {
    let changeResult = Promise.resolve(true);
    if (typeof props.beforeChange === "function") {
      const result = props.beforeChange(currentTab);
      if (typeof result !== "undefined") {
        if (result.then) {
          changeResult = result;
        } else {
          changeResult = Promise.resolve(result);
        }
      }
    }
    return changeResult;
  };
  const activeClick = (item, tabEl) => {
    if (!props.reactivable && props.modelValue === item.id) {
      return;
    }
    canChange(item.id).then((change) => {
      var _a, _b;
      if (!change) {
        return;
      }
      const tab2 = (_b = (_a = tabs2 == null ? void 0 : tabs2.state) == null ? void 0 : _a.data) == null ? void 0 : _b.find((itemOption) => itemOption.id === item.id);
      if (tabs2 && tab2 && !tab2.disabled) {
        tabs2.state.active = item.id;
        if (props.type === "slider" && tabEl && tabsEle && tabsEle.value) {
          if (["left", "right"].includes(props.tabPosition)) {
            data.offsetLeft = tabEl.getBoundingClientRect().left - tabsEle.value.nativeElement.getBoundingClientRect().left;
          } else {
            data.offsetTop = tabEl.getBoundingClientRect().top - tabsEle.value.nativeElement.getBoundingClientRect().top;
            data.offsetHeight = tabEl.getBoundingClientRect().height;
          }
          data.offsetWidth = tabEl.getBoundingClientRect().width;
        }
        ctx.emit("active-tab-change", tab2.id);
      }
    });
  };
  const beforeMount = () => {
    if (props.type !== "slider" && props.modelValue === void 0 && (tabs2 == null ? void 0 : tabs2.state.data) && tabs2.state.data.length > 0) {
      activeClick(tabs2.state.data[0]);
    }
  };
  const mounted = () => {
    var _a;
    if (props.type === "slider" && props.modelValue === void 0 && (tabs2 == null ? void 0 : tabs2.state.data) && tabs2.state.data.length > 0 && tabs2.state.data[0]) {
      const tabsStateData = tabs2.state.data[0];
      const dom = (_a = tabsStateData.tabsEle) == null ? void 0 : _a.value;
      const ele = dom == null ? void 0 : dom.getElementById(tabsStateData.tabId);
      activeClick(ele);
    }
  };
  const tabCanClose = (item) => {
    return (props.closeable || item.closeable) && !item.disabled;
  };
  return { update, activeClick, beforeMount, mounted, tabCanClose };
}
function useTabNavEvent(ctx) {
  const onTabRemove = (item, ev) => {
    ev.stopPropagation();
    ctx.emit("tab-remove", item, ev);
    ctx.emit("tab-change", item.id, "delete");
  };
  const onTabAdd = () => {
    ctx.emit("tab-add");
    ctx.emit("tab-change", void 0, "add");
  };
  return { onTabRemove, onTabAdd };
}
var tabNav = "";
var TabNav = defineComponent({
  name: "DTabNav",
  props: tabsProps,
  emits: ["active-tab-change", "tab-remove", "tab-add", "tab-change"],
  setup(props, ctx) {
    const ns2 = useNamespace("tabs");
    const tabsEle = shallowRef();
    const data = reactive({
      offsetLeft: 0,
      offsetWidth: 0,
      offsetTop: 0,
      offsetHeight: 0,
      id: null
    });
    const tabs2 = inject("tabs");
    const {
      ulClasses,
      aClasses,
      customStyle,
      sliderAnimationStyle
    } = useTabNavRender(props, data);
    const {
      update,
      beforeMount,
      mounted,
      activeClick,
      tabCanClose
    } = useTabNavFunction(props, tabs2, data, ctx, tabsEle);
    const {
      onTabRemove,
      onTabAdd
    } = useTabNavEvent(ctx);
    onUpdated(() => update());
    onBeforeMount(() => beforeMount());
    onMounted(() => mounted());
    watch(() => props.modelValue, () => {
      var _a;
      const tab2 = (_a = tabs2 == null ? void 0 : tabs2.state.data) == null ? void 0 : _a.find((item) => item.id === props.modelValue);
      if (tab2) {
        activeClick(tab2);
      }
    });
    return () => {
      const closeIconEl = (item) => {
        return tabCanClose(item) ? createVNode("span", {
          "class": ns2.e("close-btn"),
          "onClick": (ev) => onTabRemove(item, ev)
        }, [createVNode(resolveComponent("d-icon"), {
          "size": "12px",
          "name": "error-o"
        }, null)]) : null;
      };
      const newButton = props.addable ? createVNode("li", {
        "class": ns2.e("new-tab"),
        "onClick": onTabAdd
      }, [createVNode(resolveComponent("d-icon"), {
        "name": "add"
      }, null)]) : null;
      return createVNode("ul", {
        "ref": tabsEle,
        "role": "tablist",
        "class": ulClasses.value
      }, [((tabs2 == null ? void 0 : tabs2.state.data) || []).map((item, i) => {
        var _a, _b;
        return createVNode("li", {
          "role": "presentation",
          "onClick": () => {
            activeClick(item);
          },
          "class": (props.modelValue === (item.id || item.tabId) ? "active" : "") + (item.disabled ? " disabled" : ""),
          "id": item.id || item.tabId
        }, [createVNode("span", {
          "class": ns2.e("nav-content")
        }, [createVNode("a", {
          "role": "tab",
          "data-toggle": item.id,
          "aria-expanded": props.modelValue === (item.id || item.tabId),
          "class": aClasses.value,
          "style": customStyle
        }, [(tabs2 == null ? void 0 : tabs2.state.slots[i]) ? (_b = (_a = tabs2.state.slots)[i]) == null ? void 0 : _b.call(_a) : createVNode("span", null, [item.title])]), closeIconEl(item)])]);
      }), newButton, createVNode("div", {
        "class": ns2.e(`nav-${props.type}-animation`),
        "style": sliderAnimationStyle.value
      }, null)]);
    };
  }
});
const ns = useNamespace("tabs");
function useTabsEvent(ctx) {
  const onUpdateModelValue = (value) => {
    ctx.emit("update:modelValue", value);
  };
  const onActiveTabChange = (value) => {
    ctx.emit("active-tab-change", value);
  };
  const onTabRemove = (item, ev) => {
    ctx.emit("tab-remove", item, ev);
  };
  const onTabAdd = () => {
    ctx.emit("tab-add");
  };
  const onTabChange = (id, type) => {
    ctx.emit("tab-change", id, type);
  };
  return { onUpdateModelValue, onActiveTabChange, onTabRemove, onTabAdd, onTabChange };
}
function useTabsRender(props) {
  const tabsClasses = computed(() => ({
    [ns.b()]: true,
    [ns.m(props.tabPosition)]: true
  }));
  return { tabsClasses };
}
var Tabs = defineComponent({
  name: "DTabs",
  props: tabsProps,
  emits: ["update:modelValue", "active-tab-change", "tab-remove", "tab-add", "tab-change"],
  setup(props, ctx) {
    const state = reactive({
      data: [],
      active: props.modelValue,
      showContent: props.showContent,
      slots: []
    });
    provide("tabs", {
      state
    });
    const {
      onUpdateModelValue,
      onActiveTabChange,
      onTabRemove,
      onTabAdd,
      onTabChange
    } = useTabsEvent(ctx);
    const {
      tabsClasses
    } = useTabsRender(props);
    watch(() => state.active, () => {
      onUpdateModelValue(state.active);
    });
    return () => {
      var _a, _b;
      const tabNav2 = createVNode(TabNav, mergeProps(props, {
        "onActiveTabChange": onActiveTabChange,
        "onTabRemove": onTabRemove,
        "onTabAdd": onTabAdd,
        "onTabChange": onTabChange
      }), null);
      const content = (_b = (_a = ctx.slots).default) == null ? void 0 : _b.call(_a);
      return createVNode("div", {
        "class": tabsClasses.value
      }, [props.tabPosition === "bottom" ? [content, tabNav2] : [tabNav2, content], createVNode("div", {
        "style": "clear: both"
      }, null)]);
    };
  }
});
const tabProps = {
  title: {
    type: [String, Number],
    default: null
  },
  id: {
    type: String,
    default: null
  },
  disabled: {
    type: Boolean,
    default: false
  },
  closeable: {
    type: Boolean,
    default: false
  }
};
var tab = "";
var Tab = defineComponent({
  name: "DTab",
  props: tabProps,
  setup(props, {
    slots
  }) {
    var _a;
    const tabs2 = inject("tabs");
    tabs2 == null ? void 0 : tabs2.state.slots.push(slots.title);
    (_a = tabs2 == null ? void 0 : tabs2.state.data) == null ? void 0 : _a.push(props);
    const ns2 = useNamespace("tab");
    onUnmounted(() => {
      var _a2;
      if (tabs2) {
        tabs2.state.data = (_a2 = tabs2.state.data) == null ? void 0 : _a2.filter((tab2) => tab2.id !== props.id);
      }
    });
    return () => {
      var _a2;
      const {
        id
      } = props;
      const content = (tabs2 == null ? void 0 : tabs2.state.showContent) && tabs2.state.active === id ? createVNode("div", {
        "class": ns2.e("content")
      }, [createVNode("div", {
        "role": "tabpanel"
      }, [(_a2 = slots.default) == null ? void 0 : _a2.call(slots)])]) : null;
      return content;
    };
  }
});
var index = {
  title: "Tabs \u9009\u9879\u5361",
  category: "\u5BFC\u822A",
  status: "100%",
  install(app) {
    app.component(Tabs.name, Tabs);
    app.component(Tab.name, Tab);
  }
};
export { Tab, Tabs, index as default, tabsProps };
