/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var helper = require('../_chunks/dep-33ee4bce.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var config = require('../config.js');
var steps_props = require('./props.js');
var steps_stepItem = require('./step-item.js');
var utils_mixins = require('../utils/mixins.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_event = require('../utils/event.js');
require('lodash/isFunction');
require('tdesign-icons-vue');
require('./step-item-props.js');
require('../utils/render-tnode.js');
require('@babel/runtime/helpers/typeof');
require('vue');
require('lodash/isObject');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

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__default["default"](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 name = "".concat(config.prefix, "-steps");
var _Steps = utils_mixins["default"](configProvider_configReceiver["default"]("steps")).extend({
  name: "TSteps",
  components: {
    TStepItem: steps_stepItem["default"]
  },
  props: _objectSpread({}, steps_props["default"]),
  data: function data() {
    return {
      stepChildren: [],
      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() {
      if (this.direction) {
        console.warn("TDesign Steps Warn: `direction` is going to be deprecated. please use `layout` instead. ");
      }

      var layout = this.layout || this.direction || "horizontal";
      return [name, "".concat(name, "--").concat(layout), "".concat(name, "--").concat(this.handleTheme(), "-anchor"), _defineProperty__default["default"]({}, "".concat(name, "--").concat(this.sequence), layout === "vertical")];
    }
  },
  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 propsData = _objectSpread(_objectSpread({}, item), {}, {
        status: _this2.handleStatus(item, index)
      });

      var stepItem = h("t-step-item", helper.helper([{}, {
        "props": _objectSpread(_objectSpread({}, item), {}, {
          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.options;
      } else {
        options = this.getOptionListBySlots(nodes);
      }

      return options;
    },
    getOptionListBySlots: function getOptionListBySlots(nodes) {
      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;
        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 && 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 (index < matchIndex) return "finish";
      }

      var key = item.value === void 0 ? index : item.value;
      if (key === this.current) return "process";
      return "default";
    },
    addItem: function addItem(item) {
      var index = this.stepChildren.length;
      item.index = index;
      this.stepChildren.push(item);
    },
    removeItem: function removeItem(item) {
      this.stepChildren = this.stepChildren.filter(function (t) {
        return t !== item;
      });
    },
    handleChange: function handleChange(cur, prev, e) {
      utils_event.emitEvent(this, "change", cur, prev, {
        e: e
      });
    }
  }
});

exports["default"] = _Steps;
//# sourceMappingURL=steps.js.map
