/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import { h as helper } from '../_chunks/dep-42d5c87e.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import kebabCase from 'lodash/kebabCase';
import props from './props.js';
import { prefix } from '../config.js';
import _TabPanel from './tab-panel.js';
import TTabNav from './tab-nav.js';
import { emitEvent } from '../utils/event.js';
import './tab-panel-props.js';
import '../utils/render-tnode.js';
import '@babel/runtime/helpers/typeof';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/debounce';
import 'tdesign-icons-vue';
import './tab-nav-item.js';
import '../utils/ripple.js';
import '../utils/set-style.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/camelCase';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _Tabs = Vue.extend(_objectSpread(_objectSpread({
  name: "TTabs",
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    TTabPanel: _TabPanel,
    TTabNav: TTabNav
  }
}, {
  listPanels: null
}), {}, {
  props: _objectSpread({}, props),
  data: function data() {
    return {
      panels: []
    };
  },
  watch: {
    list: {
      handler: function handler() {
        this.listPanels = this.createListPanels();
      },
      deep: true
    }
  },
  mounted: function mounted() {
    this.updatePanels();
  },
  updated: function updated() {
    var _this = this;

    this.$nextTick(function () {
      _this.updatePanels();
    });
  },
  methods: {
    updatePanels: function updatePanels() {
      var _this2 = this;

      if (!this.listPanels) {
        this.panels = this.panels || [];
        return;
      }

      var newPanels = this.listPanels.map(function (panel) {
        return panel.componentInstance;
      }).filter(Boolean).filter(function (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("".concat(prefix, "-tab-panel"));
      });

      var isUnchange = function isUnchange() {
        return newPanels.length === _this2.panels.length && _this2.panels.every(function (panel, index) {
          return panel === newPanels[index];
        });
      };

      if (isUnchange()) return;
      this.panels = newPanels;
    },
    onAddTab: function onAddTab(e) {
      emitEvent(this, "add", {
        e: e
      });
    },
    onChangeTab: function onChangeTab(value) {
      emitEvent(this, "change", value);
    },
    onRemoveTab: function onRemoveTab(_ref) {
      var e = _ref.e,
          value = _ref.value,
          index = _ref.index;
      var panel = this.panels[index];
      var eventData = {
        value: value,
        index: index,
        e: e
      };
      emitEvent(this, "remove", eventData);
      if (!panel) return;
      emitEvent(panel, "remove", eventData);
    },
    renderHeader: function renderHeader() {
      var _ref2;

      var h = this.$createElement;
      var 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": (_ref2 = {}, _defineProperty(_ref2, "".concat(prefix, "-tabs__header"), true), _defineProperty(_ref2, "".concat(prefix, "-is-").concat(this.placement), true), _ref2)
      }, [h(TTabNav, helper([{}, {
        "props": tabNavProps
      }, {
        "on": {
          "change": this.onChangeTab,
          "add": this.onAddTab,
          "remove": this.onRemoveTab
        }
      }]))]);
    },
    createListPanels: function createListPanels() {
      var _this3 = this;

      var h = this.$createElement;
      return this.list.map(function (item) {
        return h(_TabPanel, helper([{}, {
          "props": _objectSpread({}, item)
        }, {
          "on": {
            "remove": _this3.onRemoveTab
          }
        }]));
      });
    },
    renderList: function renderList() {
      if (!this.listPanels) {
        return this.createListPanels();
      }

      return this.listPanels;
    },
    renderContent: function renderContent() {
      var _this$$scopedSlots$de, _this$$scopedSlots;

      var 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": ["".concat(prefix, "-tabs__content")]
      }, [this.listPanels]);
    }
  },
  render: function render() {
    var h = arguments[0];
    return h("div", {
      "class": ["".concat(prefix, "-tabs")]
    }, [this.placement !== "bottom" ? [this.renderHeader(), this.renderContent()] : [this.renderContent(), this.renderHeader()]]);
  }
}));

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