/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { h as helper } from '../../chunks/helper.js';
import Vue from 'vue';
import kebabCase from 'lodash/kebabCase';
import { emitEvent } from '../../utils/event.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import { TabsProps } from './props.js';
import _TabPanel from './tab-panel.js';
import LTabNav from './tab-nav.js';
import '../../chunks/_commonjsHelpers.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/render-node.js';
import '../../hooks/use-destroy-onclose.js';
import 'lodash/debounce';
import '@ldesign/icons-vue';
import './tab-nav-item.js';
import '../../utils/ripple.js';
import '../../chunks/set-style.js';
import '../../config.js';

const classPrefixMixins = getClassPrefixMixins("tabs");
var _Tabs = mixins(Vue, classPrefixMixins).extend({
  name: "LTabs",
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    LTabPanel: _TabPanel,
    LTabNav
  },
  ...{
    listPanels: null
  },
  props: {
    ...TabsProps
  },
  provide() {
    return {
      parent: this
    };
  },
  data() {
    return {
      panels: []
    };
  },
  watch: {
    list: {
      handler() {
        this.listPanels = this.createListPanels();
      },
      deep: true
    }
  },
  mounted() {
    this.updatePanels();
  },
  updated() {
    this.$nextTick(() => {
      this.updatePanels();
    });
  },
  methods: {
    updatePanels() {
      let {
        force = false
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      if (!this.listPanels) {
        this.panels = this.panels || [];
        return;
      }
      const newPanels = this.listPanels.map(panel => panel.componentInstance).filter(Boolean).filter(child => {
        var _child$$vnode;
        return kebabCase(child === null || child === void 0 ? void 0 : (_child$$vnode = child.$vnode) === null || _child$$vnode === void 0 ? void 0 : _child$$vnode.tag).endsWith("l-tab-panel");
      });
      const isUnchanged = () => newPanels.length === this.panels.length && this.panels.every((panel, index) => panel === newPanels[index]);
      if (isUnchanged() && !force) return;
      this.panels = newPanels;
    },
    onAddTab(e) {
      emitEvent(this, "add", {
        e
      });
    },
    onChangeTab(value) {
      emitEvent(this, "change", value);
    },
    onRemoveTab(_ref) {
      let {
        e,
        value,
        index
      } = _ref;
      const panel = this.panels[index];
      const eventData = {
        value,
        index,
        e
      };
      emitEvent(this, "remove", eventData);
      if (!panel) return;
      emitEvent(panel, "remove", eventData);
    },
    renderHeader() {
      const h = this.$createElement;
      const tabNavProps = {
        theme: this.theme,
        value: this.value,
        size: this.size,
        disabled: this.disabled,
        placement: this.placement,
        addable: this.addable,
        panels: this.panels
      };
      return h("div", {
        "class": {
          [`${this.componentName}__header`]: true,
          [`${this.classPrefix}-is-${this.placement}`]: true
        }
      }, [h("l-tab-nav", helper([{}, {
        "props": tabNavProps
      }, {
        "on": {
          "change": this.onChangeTab,
          "add": this.onAddTab,
          "remove": this.onRemoveTab
        }
      }]))]);
    },
    createListPanels() {
      const h = this.$createElement;
      return this.list.map(item => h("l-tab-panel", helper([{}, {
        "props": {
          ...item
        }
      }, {
        "on": {
          "remove": this.onRemoveTab
        }
      }])));
    },
    renderList() {
      if (!this.listPanels) return this.createListPanels();
      return this.listPanels;
    },
    renderContent() {
      var _this$$scopedSlots$de, _this$$scopedSlots;
      const h = this.$createElement;
      this.listPanels = this.list ? this.renderList() : ((_this$$scopedSlots$de = (_this$$scopedSlots = this.$scopedSlots).default) === null || _this$$scopedSlots$de === void 0 ? void 0 : _this$$scopedSlots$de.call(_this$$scopedSlots, {})) || [];
      return h("div", {
        "class": [`${this.componentName}__content`]
      }, [this.listPanels]);
    }
  },
  render() {
    const h = arguments[0];
    return h("div", {
      "class": this.componentName
    }, [this.placement !== "bottom" ? [this.renderHeader(), this.renderContent()] : [this.renderContent(), this.renderHeader()]]);
  }
});

export { _Tabs as default };
//# sourceMappingURL=tabs.js.map
