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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import { prefix } from '../config.js';
import ClASSNAMES from '../utils/classnames.js';
import { Loading } from '../loading/index.js';
import props from './props.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/set-style.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../utils/render-tnode.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/isObject';
import '../utils/transfer-dom.js';
import '../loading/props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../loading/type.js';
import '../loading/plugin.js';

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 name = "".concat(prefix, "-switch");
var _Switch = Vue.extend({
  name: "TSwitch",
  props: _objectSpread({}, props),
  model: {
    prop: "value",
    event: "change"
  },
  data: function data() {
    return {
      formDisabled: void 0
    };
  },
  computed: {
    classes: function classes() {
      var _ref;

      return ["".concat(name), ClASSNAMES.SIZE[this.size], (_ref = {}, _defineProperty(_ref, ClASSNAMES.STATUS.disabled, this.tDisabled), _defineProperty(_ref, ClASSNAMES.STATUS.loading, this.loading), _defineProperty(_ref, ClASSNAMES.STATUS.checked, this.value === this.activeValue), _ref)];
    },
    nodeClasses: function nodeClasses() {
      var _ref2;

      return ["".concat(name, "__handle"), (_ref2 = {}, _defineProperty(_ref2, ClASSNAMES.STATUS.disabled, this.tDisabled), _defineProperty(_ref2, ClASSNAMES.STATUS.loading, this.loading), _ref2)];
    },
    contentClasses: function contentClasses() {
      return ["".concat(name, "__content"), ClASSNAMES.SIZE[this.size], _defineProperty({}, ClASSNAMES.STATUS.disabled, this.tDisabled)];
    },
    tDisabled: function tDisabled() {
      return this.formDisabled || this.disabled;
    },
    activeValue: function activeValue() {
      if (this.customValue && this.customValue.length > 0) {
        return this.customValue[0];
      }

      return true;
    },
    inactiveValue: function inactiveValue() {
      if (this.customValue && this.customValue.length > 1) {
        return this.customValue[1];
      }

      return false;
    },
    content: function content() {
      if (typeof this.label === "function") {
        return this.label(this.$createElement, {
          value: this.value
        });
      }

      if (typeof this.label === "string") {
        return this.label;
      }

      if (Array.isArray(this.label)) {
        var label = this.value === this.activeValue ? this.label[0] : this.label[1];
        if (!label) return;

        if (typeof label === "string") {
          return label;
        }

        if (typeof label === "function") {
          return label(this.$createElement);
        }
      }

      if (this.$scopedSlots.label) {
        return this.$scopedSlots.label({
          value: this.value
        });
      }

      return null;
    }
  },
  watch: {
    value: {
      handler: function handler(val) {
        if (this.customValue && this.customValue.length && !this.customValue.includes(val)) {
          throw "value is not in ".concat(JSON.stringify(this.customValue));
        }
      },
      immediate: true
    }
  },
  methods: {
    handleToggle: function handleToggle() {
      var checked = this.value === this.activeValue ? this.inactiveValue : this.activeValue;
      typeof this.onChange === "function" && this.onChange(checked);
      this.$emit("change", checked);
    },
    toggle: function toggle() {
      if (this.tDisabled || this.loading) {
        return;
      }

      this.handleToggle();
    }
  },
  render: function render() {
    var h = arguments[0];
    var loading = this.loading,
        disabled = this.disabled,
        content = this.content,
        nodeClasses = this.nodeClasses,
        classes = this.classes,
        toggle = this.toggle,
        contentClasses = this.contentClasses;
    var switchContent;
    var loadingContent;

    if (loading) {
      loadingContent = h(Loading, {
        "attrs": {
          "size": "small"
        }
      });
    } else if (content) {
      switchContent = content;
    }

    return h("div", {
      "class": classes,
      "attrs": {
        "disabled": disabled
      },
      "on": {
        "click": toggle
      }
    }, [h("span", {
      "class": nodeClasses
    }, [loadingContent]), h("div", {
      "class": contentClasses
    }, [switchContent])]);
  }
});

export { _Switch as default };
//# sourceMappingURL=switch.js.map
