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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { Loading } from '../loading/index.js';
import props from './props.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import 'vue';
import '../../_chunks/dep-33b15d9d.js';
import '../../_chunks/dep-85e8b23b.js';
import '../../_chunks/dep-fc383775.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/slicedToArray';
import '../../config.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../../utils/render-node.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-a0f2090c.js';
import '../../utils/with-install.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 classPrefixMixins = getClassPrefixMixins("switch");
var _Switch = mixins(classPrefixMixins).extend({
  name: "LSwitch",
  props: _objectSpread({}, props),
  components: {
    LLoading: Loading
  },
  model: {
    prop: "value",
    event: "change"
  },
  data: function data() {
    return {
      formDisabled: void 0
    };
  },
  computed: {
    classes: function classes() {
      var _ref;

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

      return ["".concat(this.componentName, "__handle"), (_ref2 = {}, _defineProperty(_ref2, this.commonStatusClassName.disabled, this.tDisabled), _defineProperty(_ref2, this.commonStatusClassName.loading, this.loading), _ref2)];
    },
    contentClasses: function contentClasses() {
      return ["".concat(this.componentName, "__content"), this.commonSizeClassName[this.size], _defineProperty({}, this.commonStatusClassName.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("l-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
