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

'use strict';

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

var helper = require('../../chunks/helper.js');
var Vue = require('vue');
var kebabCase = require('lodash/kebabCase');
var utils_event = require('../../utils/event.js');
var components_configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../../utils/mixins.js');
var components_tabs_props = require('./props.js');
var components_tabs_tabPanel = require('./tab-panel.js');
var components_tabs_tabNav = require('./tab-nav.js');
require('../../utils/helper.js');
require('lodash/camelCase');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('../../utils/render-node.js');
require('../../hooks/use-destroy-onclose.js');
require('lodash/debounce');
require('@ldesign/icons-vue');
require('./tab-nav-item.js');
require('../../utils/ripple.js');
require('../../chunks/set-style.js');
require('../../config.js');

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

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

const classPrefixMixins = components_configProvider_configReceiver.getClassPrefixMixins("tabs");
var _Tabs = utils_mixins["default"](Vue__default["default"], classPrefixMixins).extend({
  name: "LTabs",
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    LTabPanel: components_tabs_tabPanel["default"],
    LTabNav: components_tabs_tabNav["default"]
  },
  ...{
    listPanels: null
  },
  props: {
    ...components_tabs_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__default["default"](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) {
      utils_event.emitEvent(this, "add", {
        e
      });
    },
    onChangeTab(value) {
      utils_event.emitEvent(this, "change", value);
    },
    onRemoveTab(_ref) {
      let {
        e,
        value,
        index
      } = _ref;
      const panel = this.panels[index];
      const eventData = {
        value,
        index,
        e
      };
      utils_event.emitEvent(this, "remove", eventData);
      if (!panel) return;
      utils_event.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.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.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()]]);
  }
});

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