/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var isFunction = require('lodash/isFunction');
var isString = require('lodash/isString');
var isArray = require('lodash/isArray');
var hooks_useConfig = require('../hooks/useConfig.js');
var loading_index = require('../loading/index.js');
var form_hooks = require('../form/hooks.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var switch_props = require('./props.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-7f0559c3.js');
require('dayjs');
require('../utils/withInstall.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../utils/dom.js');
require('../utils/easing.js');
require('../loading/loading.js');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../hooks/useTeleport.js');
require('../loading/props.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/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');

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

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);

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 _Switch = vue.defineComponent({
  name: "TSwitch",
  props: _objectSpread({}, switch_props["default"]),
  setup: function setup(props2, _ref) {
    var slots = _ref.slots;
    var disabled = form_hooks.useFormDisabled();
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("switch");
    var _useCommonClassName = hooks_useConfig.useCommonClassName(),
      STATUS = _useCommonClassName.STATUS,
      SIZE = _useCommonClassName.SIZE;
    var _toRefs = vue.toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue;
    var _useVModel = hooks_useVModel["default"](value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      innerValue = _useVModel2[0],
      setSwitchVal = _useVModel2[1];
    var activeValue = vue.computed(function () {
      if (props2.customValue && props2.customValue.length > 0) return props2.customValue[0];
      return true;
    });
    var inactiveValue = vue.computed(function () {
      if (props2.customValue && props2.customValue.length > 1) return props2.customValue[1];
      return false;
    });
    function handleToggle() {
      var checked = innerValue.value === activeValue.value ? inactiveValue.value : activeValue.value;
      setSwitchVal(checked);
    }
    function toggle() {
      if (disabled.value || props2.loading) return;
      handleToggle();
    }
    var classes = vue.computed(function () {
      var _ref2;
      return ["".concat(COMPONENT_NAME.value), SIZE.value[props2.size], (_ref2 = {}, _defineProperty__default["default"](_ref2, STATUS.value.disabled, disabled.value), _defineProperty__default["default"](_ref2, STATUS.value.loading, props2.loading), _defineProperty__default["default"](_ref2, STATUS.value.checked, innerValue.value === activeValue.value || props2.modelValue === activeValue.value), _ref2)];
    });
    var nodeClasses = vue.computed(function () {
      var _ref3;
      return ["".concat(COMPONENT_NAME.value, "__handle"), (_ref3 = {}, _defineProperty__default["default"](_ref3, STATUS.value.disabled, disabled.value), _defineProperty__default["default"](_ref3, STATUS.value.loading, props2.loading), _ref3)];
    });
    var contentClasses = vue.computed(function () {
      return ["".concat(COMPONENT_NAME.value, "__content"), SIZE.value[props2.size], _defineProperty__default["default"]({}, STATUS.value.disabled, disabled.value)];
    });
    vue.watch(innerValue, function (val) {
      if (props2.customValue && props2.customValue.length && !props2.customValue.includes(val)) throw new Error("value is not in ".concat(JSON.stringify(props2.customValue)));
    }, {
      immediate: true
    });
    var content = vue.computed(function () {
      if (isFunction__default["default"](props2.label)) return props2.label(vue.h, {
        value: innerValue.value
      });
      if (isString__default["default"](props2.label)) return props2.label;
      if (isArray__default["default"](props2.label) && props2.label.length) {
        var label = innerValue.value === activeValue.value ? props2.label[0] : props2.label[1];
        if (!label) return;
        if (isString__default["default"](label)) return label;
        if (isFunction__default["default"](label)) return label(vue.h);
      }
      if (slots.label) return slots.label({
        value: innerValue.value
      });
      return null;
    });
    return function () {
      var switchContent;
      var loadingContent;
      if (props2.loading) loadingContent = vue.createVNode(loading_index.Loading, {
        "size": "small"
      }, null);else if (content.value) switchContent = content.value;
      return vue.createVNode("div", vue.mergeProps({
        "class": classes.value
      }, {
        disabled: disabled.value
      }, {
        "onClick": toggle
      }), [vue.createVNode("span", {
        "class": nodeClasses.value
      }, [loadingContent]), vue.createVNode("div", {
        "class": contentClasses.value
      }, [switchContent])]);
    };
  }
});

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