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

import isNumber from 'lodash/isNumber';
import { renderContent } from '../../utils/render-node.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import props from './props.js';
import 'vue';
import 'lodash';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import '../../_chunks/dep-def4c2ce.js';

const classPrefixMixins = getClassPrefixMixins("skeleton");
const ThemeMap = {
  "text": [1],
  "avatar": [{
    type: "circle",
    size: "56px"
  }],
  "paragraph": [1, 1, {
    width: "70%"
  }],
  "avatar-text": [[{
    type: "circle"
  }, {
    type: "text",
    height: "32px"
  }]],
  "tab": [{
    height: "30px"
  }, {
    height: "200px"
  }],
  "article": [{
    type: "rect",
    height: "30px",
    width: "100%"
  }, {
    type: "rect",
    height: "200px",
    width: "100%"
  }, [{
    type: "text",
    height: "30px"
  }, {
    type: "text",
    height: "30px"
  }, {
    type: "text",
    height: "30px"
  }], [{
    type: "text",
    height: "30px"
  }, {
    type: "text",
    height: "30px"
  }], [{
    type: "text",
    height: "30px"
  }, {
    type: "text",
    height: "30px"
  }], [{
    type: "text",
    height: "30px"
  }, {
    type: "text",
    height: "30px"
  }]]
};
var _Skeleton = mixins(classPrefixMixins).extend({
  name: "LSkeleton",
  props: {
    ...props
  },
  data() {
    return {
      delayShowLoading: this.delay <= 0 ? this.loading : false,
      delayTimer: 0
    };
  },
  watch: {
    loading: {
      handler(val) {
        if (this.delay <= 0) {
          this.delayShowLoading = this.loading;
          return;
        }
        this.handleDelay(val);
      },
      immediate: true
    }
  },
  methods: {
    handleDelay(loading) {
      if (loading) {
        clearTimeout(this.delayTimer);
        this.delayTimer = window.setTimeout(() => {
          this.delayShowLoading = this.loading;
        }, this.delay);
      } else {
        this.delayShowLoading = loading;
      }
    },
    renderCols(_cols) {
      const h = this.$createElement;
      const getColItemClass = obj => [`${this.componentName}__col`, `${this.componentName}--type-${obj.type || "text"}`, {
        [`${this.componentName}--animation-${this.animation}`]: this.animation
      }];
      const getColItemStyle = obj => {
        const styleName = ["width", "height", "marginRight", "marginLeft", "margin", "size", "background", "backgroundColor"];
        const style = {};
        styleName.forEach(name => {
          if (name in obj) {
            const px = isNumber(obj[name]) ? `${obj[name]}px` : obj[name];
            if (name === "size") [style.width, style.height] = [px, px];else style[name] = px;
          }
        });
        return style;
      };
      let cols = [];
      if (Array.isArray(_cols)) cols = _cols;else if (isNumber(_cols)) cols = new Array(_cols).fill({
        type: "text"
      });else cols = [_cols];
      return cols.map(obj => h("div", {
        "class": getColItemClass(obj),
        "style": getColItemStyle(obj)
      }, [typeof obj.content === "function" ? obj.content(this.$createElement) : obj.content]));
    },
    renderRowCol(_rowCol) {
      const h = this.$createElement;
      const rowCol = _rowCol || this.rowCol;
      const getBlockClass = () => [`${this.componentName}__row`];
      return rowCol.map(item => h("div", {
        "class": getBlockClass()
      }, [this.renderCols(item)]));
    }
  },
  render() {
    const h = arguments[0];
    const content = renderContent(this, "default", "content");
    if (this.$scopedSlots.default && !this.delayShowLoading) return h("div", [content]);
    if (!this.delayShowLoading) return;
    const children = [];
    if (this.theme) children.push(this.renderRowCol(ThemeMap[this.theme]));
    if (this.rowCol) children.push(this.renderRowCol(this.rowCol));
    if (!this.theme && !this.rowCol) {
      children.push(this.renderRowCol([1, 1, 1, {
        width: "70%"
      }]));
    }
    return h("div", {
      "class": this.componentName
    }, [children]);
  }
});

export { _Skeleton as default };
//# sourceMappingURL=skeleton.js.map
