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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var config = require('../config.js');
var utils_classnames = require('../utils/classnames.js');
var loading_index = require('../loading/index.js');
var switch_props = require('./props.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../utils/render-tnode.js');
require('@babel/runtime/helpers/typeof');
require('lodash/isObject');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');

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

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

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

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

      return ["".concat(name, "__handle"), (_ref2 = {}, _defineProperty__default["default"](_ref2, utils_classnames["default"].STATUS.disabled, this.tDisabled), _defineProperty__default["default"](_ref2, utils_classnames["default"].STATUS.loading, this.loading), _ref2)];
    },
    contentClasses: function contentClasses() {
      return ["".concat(name, "__content"), utils_classnames["default"].SIZE[this.size], _defineProperty__default["default"]({}, utils_classnames["default"].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_index.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])]);
  }
});

exports["default"] = _Switch;
//# sourceMappingURL=switch.js.map
