import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import { defineComponent, ref, inject, reactive, computed, provide, toRefs, openBlock, createElementBlock, normalizeClass, createElementVNode, normalizeStyle, renderSlot } from 'vue';
import { isPlainObject, isArray, isString } from 'lodash-es';
import getPrefixCls from '../_util/getPrefixCls';
import { useTheme } from '../_theme/useTheme';
import { COMPONENT_NAME, layoutProps, LAYOUT_PROVIDE_KEY } from './const';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
const prefixCls = getPrefixCls('layout');
var script = defineComponent({
  name: COMPONENT_NAME.LAYOUT,
  props: layoutProps,
  setup(props) {
    useTheme();
    const isRoot = ref(true);
    // layout可以嵌套，所以layout也可能有父的layout
    const parent = inject(LAYOUT_PROVIDE_KEY, null);
    if (parent) {
      isRoot.value = false;
      parent.addChild({
        type: COMPONENT_NAME.LAYOUT
      });
    }
    const children = reactive([]);
    const addChild = child => {
      children.push(child);
    };
    const isHorizontal = computed(() => {
      if (children.length) {
        return children.some(node => node.type === COMPONENT_NAME.ASIDE);
      }
      return false;
    });
    const asidePlacement = computed(() => {
      if (children.length > 0) {
        if (children[0].type === COMPONENT_NAME.ASIDE) {
          return 'left';
        }
        if (children[children.length - 1].type === COMPONENT_NAME.ASIDE) {
          return 'right';
        }
      }
      return '';
    });
    const classList = computed(() => [prefixCls, isHorizontal.value && 'is-horizontal', props.fixed && 'is-fixed', isRoot.value && 'is-root'].filter(Boolean));
    const containerClassRef = computed(() => {
      const base = `${prefixCls}-container`;
      if (isPlainObject(props.containerClass)) {
        return _objectSpread({
          [base]: true
        }, props.containerClass);
      }
      if (isArray(props.containerClass)) {
        return [base, ...props.containerClass];
      }
      if (isString(props.containerClass)) {
        return [base, props.containerClass];
      }
      return [base];
    });
    provide(LAYOUT_PROVIDE_KEY, _objectSpread({
      addChild,
      asidePlacement
    }, toRefs(props)));
    return {
      prefixCls,
      classList,
      containerClassRef
    };
  }
});

function render(_ctx, _cache, $props, $setup, $data, $options) {
  return openBlock(), createElementBlock("section", {
    class: normalizeClass(_ctx.classList)
  }, [createElementVNode("div", {
    class: normalizeClass(_ctx.containerClassRef),
    style: normalizeStyle(_ctx.containerStyle)
  }, [renderSlot(_ctx.$slots, "default")], 6 /* CLASS, STYLE */)], 2 /* CLASS */);
}

script.render = render;
script.__file = "components/layout/layout.vue";

export { script as default };
