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

import { defineComponent, toRefs, inject, computed } from '@vue/composition-api';
import { CollapsePanelProps } from './props.js';
import LFakeArrow from '../common/fake-arrow.js';
import { usePrefixClass } from '../../hooks/use-config.js';
import useCollapseAnimation from './hooks/index.js';
import { renderLNodeJSX } from '../../utils/render-node.js';
import '../config-provider/config-receiver.js';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-b1584b65.js';
import '../../utils/mixins.js';
import '../config-provider/useConfig.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';

var _CollapsePanel = defineComponent({
  name: "LCollapsePanel",
  props: CollapsePanelProps,
  components: {
    LFakeArrow
  },

  setup(props2) {
    const {
      value,
      disabled,
      expandIcon
    } = toRefs(props2);
    const componentName = usePrefixClass("collapse-panel");
    const disableClass = usePrefixClass("is-disabled");
    const clickableClass = usePrefixClass("is-clickable");
    const transitionClass = usePrefixClass("slide-down");
    const collapseValue = inject("collapseValue");
    const updateCollapseValue = inject("updateCollapseValue");
    const getUniqId = inject("getUniqId", () => void 0, false);
    const {
      defaultExpandAll,
      disabled: disableAll,
      expandIconPlacement,
      expandOnRowClick,
      expandIcon: expandIconAll
    } = inject("collapseProps");
    const innerValue = value.value || getUniqId();
    const showExpandIcon = computed(() => expandIcon.value === void 0 ? expandIconAll.value : expandIcon.value);

    if (defaultExpandAll.value) {
      updateCollapseValue(innerValue);
    }

    const isDisabled = computed(() => disabled.value || disableAll.value);
    const isActive = computed(() => collapseValue.value instanceof Array ? collapseValue.value.includes(innerValue) : collapseValue.value === innerValue);
    const classes = computed(() => [componentName.value, {
      [disableClass.value]: isDisabled.value
    }]);
    return {
      isDisabled,
      classes,
      isActive,
      updateCollapseValue,
      innerValue,
      expandIconPlacement,
      expandOnRowClick,
      componentName,
      clickableClass,
      transitionClass,
      showExpandIcon
    };
  },

  methods: {
    renderIcon(direction) {
      const h = this.$createElement;
      return h("l-fake-arrow", {
        "attrs": {
          "name": "arrow",
          "isActive": this.isActive,
          "overlayClassName": `${this.componentName}__icon ${this.componentName}__icon--${direction}`
        }
      });
    },

    renderHeader() {
      const h = this.$createElement;
      const {
        showExpandIcon,
        isDisabled,
        handleClick,
        renderIcon,
        renderBlank
      } = this;
      const cls = [`${this.componentName}__header`, {
        [this.clickableClass]: this.expandOnRowClick && !isDisabled
      }];
      return h("div", {
        "ref": "headRef",
        "class": cls,
        "on": {
          "click": handleClick
        }
      }, [h("div", {
        "class": `${this.componentName}__header-left`
      }, [showExpandIcon && this.expandIconPlacement === "left" ? renderIcon(this.expandIconPlacement) : null]), h("div", {
        "class": `${this.componentName}__header-content`
      }, [renderLNodeJSX(this, "header")]), renderBlank(), h("div", {
        "class": `${this.componentName}__header-right`
      }, [renderLNodeJSX(this, "headerRightContent"), showExpandIcon && this.expandIconPlacement === "right" ? renderIcon(this.expandIconPlacement) : null])]);
    },

    renderBlank() {
      const h = this.$createElement;
      return h("div", {
        "class": `${this.componentName}__header--blank`
      });
    },

    renderBody() {
      return this.destroyOnCollapse ? this.renderBodyDestroyOnCollapse() : this.renderBodyByNormal();
    },

    renderBodyByNormal() {
      const h = this.$createElement;
      return h("div", {
        "directives": [{
          name: "show",
          value: this.isActive
        }],
        "class": `${this.componentName}__body`
      }, [h("div", {
        "class": `${this.componentName}__content`
      }, [renderLNodeJSX(this, "default")])]);
    },

    renderBodyDestroyOnCollapse() {
      const h = this.$createElement;
      return this.isActive ? h("div", {
        "class": `${this.componentName}__body`
      }, [h("div", {
        "class": `${this.componentName}__content`
      }, [renderLNodeJSX(this, "default")])]) : null;
    },

    handleClick(e) {
      const canExpand = this.expandOnRowClick && e.target === this.$refs.headRef || e.target.getAttribute("name") === "arrow";

      if (canExpand && !this.isDisabled) {
        this.updateCollapseValue(this.innerValue);
      }
    }

  },

  render() {
    const h = arguments[0];
    const {
      beforeEnter,
      enter,
      afterEnter,
      beforeLeave,
      leave,
      afterLeave
    } = useCollapseAnimation();
    const {
      classes
    } = this;
    return h("div", {
      "class": classes
    }, [h("div", {
      "class": `${this.componentName}__wrapper`
    }, [this.renderHeader(), h("transition", {
      "attrs": {
        "name": this.transitionClass
      },
      "on": {
        "beforeEnter": beforeEnter,
        "enter": enter,
        "afterEnter": afterEnter,
        "beforeLeave": beforeLeave,
        "leave": leave,
        "afterLeave": afterLeave
      }
    }, [this.renderBody()])])]);
  }

});

export { _CollapsePanel as default };
//# sourceMappingURL=collapse-panel.js.map
