/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import { h as helper } from '../../chunks/helper.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { StepsProps } from './props.js';
import _StepItem from './step-item.js';
import mixins from '../../utils/mixins.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import { emitEvent } from '../../utils/event.js';
import '../../chunks/_commonjsHelpers.js';
import 'lodash/isFunction';
import '@ldesign/icons-vue';
import '../../utils/render-node.js';
import '@babel/runtime/helpers/typeof';
import 'vue';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _Steps = mixins(getConfigReceiverMixins("steps")).extend({
  name: "LSteps",
  components: {
    TStepItem: _StepItem
  },
  props: _objectSpread({}, StepsProps),
  data: function data() {
    return {
      indexMap: {}
    };
  },
  provide: function provide() {
    return {
      steps: this
    };
  },
  watch: {
    options: {
      immediate: true,
      handler: function handler() {
        var _this = this;

        if (!this.options) return;
        this.options.forEach(function (item, index) {
          if (item.value !== void 0) {
            _this.indexMap[item.value] = index;
          }
        });
      }
    }
  },
  computed: {
    baseClass: function baseClass() {
      return [this.componentName, "".concat(this.componentName, "--").concat(this.layout), "".concat(this.componentName, "--").concat(this.sequence), "".concat(this.componentName, "--").concat(this.handleTheme(), "-anchor"), "".concat(this.componentName, "--").concat(this.separator, "-separator")];
    }
  },
  render: function render() {
    var _this$$scopedSlots,
        _this2 = this;

    var h = arguments[0];
    var nodes = ((_this$$scopedSlots = this.$scopedSlots) === null || _this$$scopedSlots === void 0 ? void 0 : _this$$scopedSlots["default"]) && this.$scopedSlots["default"](null);
    var options = this.getOptions();
    var content = options.map(function (item, index) {
      var stepIndex = _this2.sequence === "reverse" ? options.length - index - 1 : index;

      var propsData = _objectSpread(_objectSpread({}, item), {}, {
        index: stepIndex,
        status: _this2.handleStatus(item, index)
      });

      var stepItem = h("t-step-item", helper([{}, {
        "props": _objectSpread(_objectSpread({}, item), {}, {
          index: stepIndex,
          status: _this2.handleStatus(item, index)
        })
      }, {
        "key": item.value || index
      }]));

      if (nodes && nodes[index]) {
        var 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: function getOptions() {
      var _this$$scopedSlots2;

      var nodes = ((_this$$scopedSlots2 = this.$scopedSlots) === null || _this$$scopedSlots2 === void 0 ? void 0 : _this$$scopedSlots2["default"]) && this.$scopedSlots["default"](null);
      var options;

      if (this.options && this.options.length) {
        options = this.sequence === "reverse" ? this.options.reverse() : this.options;
      } else {
        options = this.getOptionListBySlots(nodes);
      }

      return options;
    },
    getOptionListBySlots: function getOptionListBySlots(nodes) {
      var _this3 = this;

      var arr = [];
      nodes === null || nodes === void 0 ? void 0 : nodes.forEach(function (node) {
        var _node$componentOption;

        var 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;
        _this3.sequence === "reverse" ? arr.unshift(option) : arr.push(option);
      });
      return arr;
    },
    handleTheme: function handleTheme() {
      var theme = this.theme;
      var options = this.getOptions();
      options.forEach(function (item) {
        if ((item === null || item === void 0 ? void 0 : item.icon) !== void 0) {
          theme = "default";
        }
      });
      return theme;
    },
    handleStatus: function 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) {
        var matchIndex = this.indexMap[this.current];

        if (matchIndex === void 0) {
          console.warn("TDesign Steps Warn: The current `value` is not exist.");
          return "default";
        }

        if (this.sequence === "positive" && index < matchIndex) return "finish";
        if (this.sequence === "reverse" && index > matchIndex) return "finish";
      }

      var key = item.value === void 0 ? index : item.value;
      if (key === this.current) return "process";
      return "default";
    },
    handleChange: function handleChange(cur, prev, e) {
      emitEvent(this, "change", cur, prev, {
        e: e
      });
    }
  }
});

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