/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var helper = require('../_chunks/dep-33ee4bce.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var kebabCase = require('lodash/kebabCase');
var tabs_props = require('./props.js');
var config = require('../config.js');
var tabs_tabPanel = require('./tab-panel.js');
var tabs_tabNav = require('./tab-nav.js');
var utils_event = require('../utils/event.js');
require('./tab-panel-props.js');
require('../utils/render-tnode.js');
require('@babel/runtime/helpers/typeof');
require('lodash/isObject');
require('../hooks/useDestroyOnClose.js');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/debounce');
require('tdesign-icons-vue');
require('./tab-nav-item.js');
require('../utils/mixins.js');
require('../utils/ripple.js');
require('../_common/js/utils/set-style.js');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/camelCase');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var kebabCase__default = /*#__PURE__*/_interopDefaultLegacy(kebabCase);

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__default["default"](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__default["default"].extend(_objectSpread(_objectSpread({
  name: "TTabs",
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    TTabPanel: tabs_tabPanel["default"],
    TTabNav: tabs_tabNav["default"]
  }
}, {
  listPanels: null
}), {}, {
  props: _objectSpread({}, tabs_props["default"]),
  provide: function provide() {
    return {
      parent: this
    };
  },
  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;

      var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          _ref$force = _ref.force,
          force = _ref$force === void 0 ? false : _ref$force;

      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__default["default"](child === null || child === void 0 ? void 0 : (_child$$vnode = child.$vnode) === null || _child$$vnode === void 0 ? void 0 : _child$$vnode.tag).endsWith("".concat(config.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() && !force) return;
      this.panels = newPanels;
    },
    onAddTab: function onAddTab(e) {
      utils_event.emitEvent(this, "add", {
        e: e
      });
    },
    onChangeTab: function onChangeTab(value) {
      utils_event.emitEvent(this, "change", value);
    },
    onRemoveTab: function onRemoveTab(_ref2) {
      var e = _ref2.e,
          value = _ref2.value,
          index = _ref2.index;
      var panel = this.panels[index];
      var eventData = {
        value: value,
        index: index,
        e: e
      };
      utils_event.emitEvent(this, "remove", eventData);
      if (!panel) return;
      utils_event.emitEvent(panel, "remove", eventData);
    },
    renderHeader: function renderHeader() {
      var _ref3;

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

exports["default"] = _Tabs;
//# sourceMappingURL=tabs.js.map
