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

import isObject from 'lodash/isObject';
import { calcSize } from '../../utils/responsive.js';
import { renderLNodeJSX } from '../../utils/render-node.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import { ColProps } from './props.js';
import 'vue';
import 'lodash';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';

const classPrefixMixins = getClassPrefixMixins("col");
var _Col = mixins(classPrefixMixins).extend({
  name: "LCol",
  props: {
    ...ColProps
  },
  inject: ["rowContext"],
  data() {
    return {
      size: "md"
    };
  },
  computed: {
    classes() {
      const {
        span,
        order,
        offset,
        push,
        pull
      } = this;
      const allSizes = ["xs", "sm", "md", "lg", "xl", "xxl"];
      const sizeClasses = allSizes.reduce((acc, currSize) => {
        const sizeProp = this[currSize];
        let sizeObject = {};
        if (typeof sizeProp === "number") sizeObject.span = sizeProp;else if (isObject(sizeProp)) sizeObject = sizeProp || {};
        return {
          ...acc,
          [`${this.componentName}-${currSize}-${sizeObject.span}`]: sizeObject.span !== void 0,
          [`${this.componentName}-${currSize}-order-${sizeObject.order}`]: parseInt(sizeObject.order, 10) >= 0,
          [`${this.componentName}-${currSize}-offset-${sizeObject.offset}`]: parseInt(sizeObject.offset, 10) >= 0,
          [`${this.componentName}-${currSize}-push-${sizeObject.push}`]: parseInt(sizeObject.push, 10) >= 0,
          [`${this.componentName}-${currSize}-pull-${sizeObject.pull}`]: parseInt(sizeObject.pull, 10) >= 0
        };
      }, {});
      return {
        [`${this.componentName}`]: true,
        [`${this.componentName}-${span}`]: span !== void 0,
        [`${this.componentName}-order-${order}`]: order,
        [`${this.componentName}-offset-${offset}`]: offset,
        [`${this.componentName}-push-${push}`]: push,
        [`${this.componentName}-pull-${pull}`]: pull,
        ...sizeClasses
      };
    }
  },
  mounted() {
    this.updateSize();
    window.addEventListener("resize", this.updateSize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.updateSize);
  },
  methods: {
    updateSize() {
      this.size = calcSize(window.innerWidth);
    },
    parseFlex(flex) {
      if (typeof flex === "number") return `${flex} ${flex} 0`;
      if (/^\d+(\.\d+)?(px|r?em|%)$/.test(flex)) return `0 0 ${flex}`;
      return flex;
    },
    calcColPadding(gutter, currentSize) {
      const paddingObj = {};
      if (typeof gutter === "number") {
        Object.assign(paddingObj, {
          paddingLeft: `${gutter / 2}px`,
          paddingRight: `${gutter / 2}px`
        });
      } else if (Array.isArray(gutter) && gutter.length) {
        if (typeof gutter[0] === "number") {
          Object.assign(paddingObj, {
            paddingLeft: `${gutter[0] / 2}px`,
            paddingRight: `${gutter[0] / 2}px`
          });
        }
        if (isObject(gutter[0]) && gutter[0][currentSize]) {
          Object.assign(paddingObj, {
            paddingLeft: `${gutter[0][currentSize] / 2}px`,
            paddingRight: `${gutter[0][currentSize] / 2}px`
          });
        }
      } else if (isObject(gutter) && gutter[currentSize]) {
        Object.assign(paddingObj, {
          paddingLeft: `${gutter[currentSize] / 2}px`,
          paddingRight: `${gutter[currentSize] / 2}px`
        });
      }
      return paddingObj;
    }
  },
  render() {
    const h = arguments[0];
    const {
      flex,
      tag,
      classes
    } = this;
    const colStyle = {};
    flex && (colStyle.flex = this.parseFlex(flex));
    const {
      rowContext
    } = this;
    if (rowContext) {
      const {
        gutter: rowGutter
      } = rowContext;
      Object.assign(colStyle, this.calcColPadding(rowGutter, this.size));
    }
    return h(tag, {
      "class": classes,
      "style": colStyle
    }, [renderLNodeJSX(this, "default")]);
  }
});

export { _Col as default };
//# sourceMappingURL=col.js.map
