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

import { defineComponent, toRefs, inject, computed } from '@vue/composition-api';
import FakeArrow from '../common/fake-arrow.js';
import { usePrefixClass } from '../../hooks/use-config.js';
import { useLNodeJSX, useContent } from '../../hooks/lnode.js';
import { CollapsePanelProps } from './props.js';
import useCollapseAnimation from './hooks/index.js';
import '../config-provider/config-receiver.js';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-def4c2ce.js';
import '../../utils/mixins.js';
import '../config-provider/useConfig.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../hooks/render-lnode.js';

var _CollapsePanel = defineComponent({
  name: "LCollapsePanel",
  props: CollapsePanelProps,
  setup(props2, _ref) {
    let {
      slots
    } = _ref;
    const {
      value,
      disabled
    } = 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
    } = inject("collapseProps");
    const renderParentTNode = inject("renderParentTNode");
    const innerValue = value.value || getUniqId();
    if (defaultExpandAll.value) updateCollapseValue(innerValue);
    const isDisabled = computed(() => disabled.value || disableAll.value);
    const isActive = computed(() => Array.isArray(collapseValue.value) ? collapseValue.value.includes(innerValue) : collapseValue.value === innerValue);
    const classes = computed(() => [componentName.value, {
      [disableClass.value]: isDisabled.value
    }]);
    const panelExpandIcon = computed(() => slots.expandIcon || props2.expandIcon);
    const renderTNodeJSX = useLNodeJSX();
    const renderContent = useContent();
    return {
      isDisabled,
      classes,
      isActive,
      updateCollapseValue,
      innerValue,
      expandIconPlacement,
      expandOnRowClick,
      componentName,
      clickableClass,
      transitionClass,
      panelExpandIcon,
      renderParentTNode,
      renderTNodeJSX,
      renderContent
    };
  },
  methods: {
    renderDefaultIcon() {
      const h = this.$createElement;
      const {
        componentName
      } = this;
      return h(FakeArrow, {
        "attrs": {
          "overlayClassName": `${componentName}__icon--default`
        }
      });
    },
    renderIcon() {
      const h = this.$createElement;
      const {
        panelExpandIcon,
        renderParentTNode,
        componentName,
        expandIconPlacement,
        isActive,
        handleClick,
        renderDefaultIcon,
        renderTNodeJSX
      } = this;
      const tNodeRender = panelExpandIcon === void 0 ? renderParentTNode : renderTNodeJSX;
      return h("div", {
        "ref": "iconRef",
        "class": `${componentName}__icon ${componentName}__icon--${expandIconPlacement} ${isActive ? `${componentName}__icon--active` : ""}`,
        "on": {
          "click": handleClick
        }
      }, [tNodeRender("expandIcon", renderDefaultIcon())]);
    },
    renderHeader() {
      const h = this.$createElement;
      const {
        isDisabled,
        handleClick,
        renderIcon,
        renderBlank
      } = this;
      const cls = [`${this.componentName}__header`, {
        [this.clickableClass]: this.expandOnRowClick && !isDisabled
      }];
      return h("div", {
        "class": cls,
        "on": {
          "click": handleClick
        }
      }, [h("div", {
        "class": `${this.componentName}__header-left`
      }, [this.expandIconPlacement === "left" && renderIcon()]), h("div", {
        "class": `${this.componentName}__header-content`
      }, [this.renderTNodeJSX("header")]), renderBlank(), h("div", {
        "class": `${this.componentName}__header-right`
      }, [h("div", {
        "class": `${this.componentName}__header-right-content`,
        "on": {
          "click": e => e.stopPropagation()
        }
      }, [this.renderTNodeJSX("headerRightContent")]), this.expandIconPlacement === "right" && renderIcon()])]);
    },
    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`
      }, [this.renderContent("default", "content")])]);
    },
    renderBodyDestroyOnCollapse() {
      const h = this.$createElement;
      return this.isActive ? h("div", {
        "class": `${this.componentName}__body`
      }, [h("div", {
        "class": `${this.componentName}__content`
      }, [this.renderContent("default", "content")])]) : null;
    },
    handleClick(e) {
      const {
        expandOnRowClick,
        updateCollapseValue,
        innerValue,
        isDisabled
      } = this;
      const canExpand = expandOnRowClick || e.currentTarget === this.$refs.iconRef;
      if (canExpand && !isDisabled) updateCollapseValue(innerValue);
      e.stopPropagation();
    }
  },
  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
