/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import { h as helper } from '../../_chunks/dep-b13ef886.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import kebabCase from 'lodash/kebabCase';
import { TabsProps } from './props.js';
import _TabPanel from './tab-panel.js';
import LTabNav from './tab-nav.js';
import { emitEvent } from '../../utils/event.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import '../../utils/render-node.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/camelCase';
import '../../hooks/use-destroy-onclose.js';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/debounce';
import '@ldesign/icons-vue';
import './tab-nav-item.js';
import '../../utils/ripple.js';
import '../../_chunks/dep-85e8b23b.js';
import '../../config.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-a0f2090c.js';

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 classPrefixMixins = getClassPrefixMixins("tabs");
var _Tabs = mixins(Vue, classPrefixMixins).extend(_objectSpread(_objectSpread({
  name: "LTabs",
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    LTabPanel: _TabPanel,
    LTabNav: LTabNav
  }
}, {
  listPanels: null
}), {}, {
  props: _objectSpread({}, TabsProps),
  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(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");
      });

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

      if (isUnchanged() && !force) return;
      this.panels = newPanels;
    },
    onAddTab: function onAddTab(e) {
      emitEvent(this, "add", {
        e: e
      });
    },
    onChangeTab: function onChangeTab(value) {
      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
      };
      emitEvent(this, "remove", eventData);
      if (!panel) return;
      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(_ref3, "".concat(this.componentName, "__header"), true), _defineProperty(_ref3, "".concat(this.classPrefix, "-is-").concat(this.placement), true), _ref3)
      }, [h("l-tab-nav", 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("l-tab-panel", 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(this.componentName, "__content")]
      }, [this.listPanels]);
    }
  },
  render: function render() {
    var 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
