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

import { h as helper } from '../../_chunks/dep-29915b8e.js';
import { l as log } from '../../_chunks/dep-70c789e5.js';
import mixins from '../../utils/mixins.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import { emitEvent } from '../../utils/event.js';
import _StepItem from './step-item.js';
import { StepsProps } from './props.js';
import '../../_chunks/dep-5210251b.js';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-def4c2ce.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import 'lodash/isFunction';
import '@ldesign/icons-vue';
import '../../utils/render-node.js';

var _Steps = mixins(getConfigReceiverMixins("steps")).extend({
  name: "LSteps",
  components: {
    TStepItem: _StepItem
  },
  props: {
    ...StepsProps
  },
  data() {
    return {
      indexMap: {}
    };
  },
  provide() {
    return {
      steps: this
    };
  },
  watch: {
    options: {
      immediate: true,
      handler() {
        if (!this.options) return;
        this.options.forEach((item, index) => {
          if (item.value !== void 0) this.indexMap[item.value] = index;
        });
      }
    }
  },
  computed: {
    baseClass() {
      return [this.componentName, `${this.componentName}--${this.layout}`, `${this.componentName}--${this.sequence}`, `${this.componentName}--${this.handleTheme()}-anchor`, `${this.componentName}--${this.separator}-separator`];
    }
  },
  render() {
    var _this$$scopedSlots;
    const h = arguments[0];
    const nodes = ((_this$$scopedSlots = this.$scopedSlots) === null || _this$$scopedSlots === void 0 ? void 0 : _this$$scopedSlots.default) && this.$scopedSlots.default(null);
    const options = this.getOptions();
    const content = options.map((item, index) => {
      const stepIndex = this.sequence === "reverse" ? options.length - index - 1 : index;
      const propsData = {
        ...item,
        index: stepIndex,
        status: this.handleStatus(item, index)
      };
      const stepItem = h("t-step-item", helper([{}, {
        "props": {
          ...item,
          index: stepIndex,
          status: this.handleStatus(item, index)
        }
      }, {
        "key": item.value || index
      }]));
      if (nodes && nodes[index]) {
        const vnode = nodes[index];
        if (vnode.componentOptions) {
          vnode.componentOptions.propsData = propsData;
          return vnode;
        }
        return stepItem;
      }
      return stepItem;
    });
    return h("div", {
      "class": this.baseClass
    }, [content]);
  },
  methods: {
    getOptions() {
      var _this$$scopedSlots2;
      const nodes = ((_this$$scopedSlots2 = this.$scopedSlots) === null || _this$$scopedSlots2 === void 0 ? void 0 : _this$$scopedSlots2.default) && this.$scopedSlots.default(null);
      let options;
      if (this.options && this.options.length) options = this.sequence === "reverse" ? this.options.reverse() : this.options;else options = this.getOptionListBySlots(nodes);
      return options;
    },
    getOptionListBySlots(nodes) {
      const arr = [];
      nodes === null || nodes === void 0 ? void 0 : nodes.forEach(node => {
        var _node$componentOption;
        const option = node === null || node === void 0 ? void 0 : (_node$componentOption = node.componentOptions) === null || _node$componentOption === void 0 ? void 0 : _node$componentOption.propsData;
        if (!option) return;
        this.sequence === "reverse" ? arr.unshift(option) : arr.push(option);
      });
      return arr;
    },
    handleTheme() {
      let {
        theme
      } = this;
      const options = this.getOptions();
      options.forEach(item => {
        if ((item === null || item === void 0 ? void 0 : item.icon) !== void 0) {
          theme = "default";
        }
      });
      return theme;
    },
    handleStatus(item, index) {
      if (item.status && item.status !== "default") return item.status;
      if (this.current === "FINISH") return "finish";
      if (item.value === void 0) {
        if (this.sequence === "positive" && index < this.current) return "finish";
        if (this.sequence === "reverse" && index > this.current) return "finish";
      }
      if (item.value !== void 0) {
        const matchIndex = this.indexMap[this.current];
        if (matchIndex === void 0) {
          log.warn("Step", "The current `value` is not exist.");
          return "default";
        }
        if (this.sequence === "positive" && index < matchIndex) return "finish";
        if (this.sequence === "reverse" && index > matchIndex) return "finish";
      }
      const key = item.value === void 0 ? index : item.value;
      if (key === this.current) return "process";
      return "default";
    },
    handleChange(cur, prev, e) {
      emitEvent(this, "change", cur, prev, {
        e
      });
    }
  }
});

export { _Steps as default };
//# sourceMappingURL=steps.js.map
