/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import '../../chunks/index.js';
import { Loading } from '../loading/index.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import props from './props.js';
import { l as log } from '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../utils/with-install.js';
import 'vue';
import 'lodash';
import '../../config.js';
import '../loading/loading.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../../utils/render-node.js';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../../chunks/defineProperty-838e4192.js';
import '../../chunks/set-style.js';
import '../../chunks/helper2.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../loading/style';
import '../loading/type.js';
import '../loading/plugin.js';

const classPrefixMixins = getClassPrefixMixins("switch");
var _Switch = mixins(classPrefixMixins).extend({
  name: "LSwitch",
  props: {
    ...props
  },
  components: {
    LLoading: Loading
  },
  model: {
    prop: "value",
    event: "change"
  },
  data() {
    return {
      formDisabled: void 0
    };
  },
  computed: {
    classes() {
      return [`${this.componentName}`, this.commonSizeClassName[this.size], {
        [this.commonStatusClassName.disabled]: this.tDisabled,
        [this.commonStatusClassName.loading]: this.loading,
        [this.commonStatusClassName.checked]: this.value === this.activeValue
      }];
    },
    nodeClasses() {
      return [`${this.componentName}__handle`, {
        [this.commonStatusClassName.disabled]: this.tDisabled,
        [this.commonStatusClassName.loading]: this.loading
      }];
    },
    contentClasses() {
      return [`${this.componentName}__content`, this.commonSizeClassName[this.size], {
        [this.commonStatusClassName.disabled]: this.tDisabled
      }];
    },
    tDisabled() {
      return this.formDisabled || this.disabled;
    },
    activeValue() {
      if (this.customValue && this.customValue.length > 0) return this.customValue[0];
      return true;
    },
    inactiveValue() {
      if (this.customValue && this.customValue.length > 1) return this.customValue[1];
      return false;
    },
    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)) {
        const 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(val) {
        if (this.customValue && this.customValue.length && !this.customValue.includes(val)) log.error("Switch", `value is not in ${JSON.stringify(this.customValue)}`);
      },
      immediate: true
    }
  },
  methods: {
    handleToggle() {
      const checked = this.value === this.activeValue ? this.inactiveValue : this.activeValue;
      typeof this.onChange === "function" && this.onChange(checked);
      this.$emit("change", checked);
    },
    toggle() {
      if (this.tDisabled || this.loading) return;
      this.handleToggle();
    }
  },
  render() {
    const h = arguments[0];
    const {
      loading,
      disabled,
      content,
      nodeClasses,
      classes,
      toggle,
      contentClasses
    } = this;
    let switchContent;
    let 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
