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

'use strict';

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

var log = require('../../chunks/log.js');
var components_loading_index = require('../loading/index.js');
var components_configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../../utils/mixins.js');
var components_switch_props = require('./props.js');
require('../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../utils/with-install.js');
require('vue');
require('lodash');
require('../../config.js');
require('../loading/loading.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../utils/render-node.js');
require('../../utils/transfer-dom.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../../chunks/defineProperty-838e4192.js');
require('../../chunks/set-style.js');
require('../../chunks/helper2.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('../loading/plugin.js');

const classPrefixMixins = components_configProvider_configReceiver.getClassPrefixMixins("switch");
var _Switch = utils_mixins["default"](classPrefixMixins).extend({
  name: "LSwitch",
  props: {
    ...components_switch_props["default"]
  },
  components: {
    LLoading: components_loading_index.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.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])]);
  }
});

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