"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _button = _interopRequireDefault(require("antd/lib/button"));
var _switch = _interopRequireDefault(require("antd/lib/switch"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var React = _interopRequireWildcard(require("react"));
var _index = require("@ant-design/icons/es/index");
require("./index.less");
var _excluded = ["className", "style", "checked", "defaultChecked", "disabled", "size", "type", "loading", "checkedChildren", "unCheckedChildren", "buttonname", "disableType", "checolors", "conentColor", "fontsizes"];
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var antSwitch = function antSwitch(_ref) {
  var className = _ref.className,
    style = _ref.style,
    checked = _ref.checked,
    defaultChecked = _ref.defaultChecked,
    disabled = _ref.disabled,
    size = _ref.size,
    type = _ref.type,
    loading = _ref.loading,
    checkedChildren = _ref.checkedChildren,
    unCheckedChildren = _ref.unCheckedChildren,
    buttonname = _ref.buttonname,
    disableType = _ref.disableType,
    checolors = _ref.checolors,
    conentColor = _ref.conentColor,
    fontsizes = _ref.fontsizes,
    otherProps = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
  var _useState = (0, React.useState)(''),
    ID = _useState[0],
    setID = _useState[1];
  var _useState2 = (0, React.useState)(),
    colorOpen = _useState2[0],
    setColorOpen = _useState2[1];
  var _useState3 = (0, React.useState)(),
    handles = _useState3[0],
    sethandles = _useState3[1];
  var _useState4 = (0, React.useState)(),
    leftstyles = _useState4[0],
    setleftstyle = _useState4[1];
  var doc = document.getElementById(ID) || document;
  // const [_style,setStyles] =useState({width:"44px"})
  var _style = (0, _extends2["default"])({}, style) || {};
  var _styles = (0, _extends2["default"])({}, style) || {};
  var _otherProps = (0, _extends2["default"])({}, otherProps) || {};
  _style['marginLeft'] = 'none';
  _style['marginRight'] = 'none';
  _style['marginTop'] = 'none';
  _style['marginBottom'] = 'none';
  _style['paddingLeft'] = 'none';
  _style['paddingRight'] = 'none';
  _style['paddingTop'] = 'none';
  _style['paddingBottom'] = 'none';
  _otherProps.style = _style;
  _styles['width'] = "none";
  (0, React.useEffect)(function () {
    setID("switch_" + uuid());
  }, []);
  (0, React.useEffect)(function () {
    checolor();
    switchhandle();
    content();
  });
  function uuid() {
    return (Math.random() * 1e6 >> 0).toString(36);
  }
  var onChange = function onChange(checked) {
    console.log("switch to " + checked);
    if (checked) {
      checolor();
    } else {}
  };
  var defaultProps = (0, _extends2["default"])({
    size: size,
    style: style,
    disabled: disabled,
    loading: loading,
    defaultChecked: defaultChecked,
    buttonname: buttonname,
    onChange: onChange,
    checolors: checolors,
    conentColor: conentColor,
    fontsizes: fontsizes
  }, otherProps);
  if (type == "文本") {
    defaultProps.checkedChildren = checkedChildren;
    defaultProps.unCheckedChildren = unCheckedChildren;
  } else if (type == "icon") {
    defaultProps.checkedChildren = /*#__PURE__*/React.createElement(_index.CheckOutlined, null);
    defaultProps.unCheckedChildren = /*#__PURE__*/React.createElement(_index.CloseOutlined, null);
  }
  if (checked) {
    defaultProps.checked = checked;
  }

  //修改div
  var divstyle = function divstyle() {
    setTimeout(function () {
      var res = document.getElementById(ID);
      console.log(res);
    }, 0);
  };
  // 修改选中颜色
  var checolor = function checolor() {
    setColorOpen(checolors);
    var elem = doc.querySelector('.ant-switch-checked');
    var style = document.createElement("style"); //创建一个<style>标签
    var openStyle = document.createTextNode('#' + ID + ' ' + '.ant-switch-checked{background-color:' + colorOpen + '!important' + ";}"); //更改后的样式
    style.appendChild(openStyle); //把样式添加到style标签里
    document.body.appendChild(style); //把内联样式表添加到html中
    if (elem) {
      elem.classList.add("openStyle");
      // if(checolors){
      //   elem.classList.add("openStyle");
      // }else{
      //   elem.classList.remove("openStyle");
      // }
    }
  };

  // 开关等比例缩放
  var switchhandle = function switchhandle() {
    var _style$height, _style$height2;
    // 动态获取单位
    var unit = style !== null && style !== void 0 && (_style$height = style.height) !== null && _style$height !== void 0 && _style$height.match(/\d+(\D+)/)[1] ? style === null || style === void 0 ? void 0 : (_style$height2 = style.height) === null || _style$height2 === void 0 ? void 0 : _style$height2.match(/\d+(\D+)/)[1] : "px";
    sethandles(Math.round(parseInt(style === null || style === void 0 ? void 0 : style.height) / 2));
    setleftstyle(parseInt(style === null || style === void 0 ? void 0 : style.height) - 2);
    if ((style === null || style === void 0 ? void 0 : style.height) == "100%") {
      sethandles(NaN);
      setleftstyle(NaN);
    }
    var elem = doc.querySelector('.ant-switch-handle');
    var elems = doc.querySelector('.ant-switch-checked .ant-switch-handle');
    elem.style.width = parseInt(style === null || style === void 0 ? void 0 : style.height) - 4 + unit;
    elem.style.height = parseInt(style === null || style === void 0 ? void 0 : style.height) - 4 + unit;
    var styles = document.createElement("style"); //创建一个<style>标签
    var openhandles = document.createTextNode('#' + ID + ' ' + '.ant-switch-handle:before{border-radius:' + handles + unit + ";}"); //更改后before的样式
    var leftstyle = document.createTextNode('#' + ID + ' ' + '.ant-switch-checked .ant-switch-handle{left : calc(100% - ' + leftstyles + unit + ')' + ";}"); //更改后leftstyle的样式
    styles.appendChild(openhandles); //把样式添加到style标签里
    styles.appendChild(leftstyle);
    document.body.appendChild(styles); //把内联样式表添加到html中
    if (elem) {
      elem.classList.add("openhandles");
      elem.classList.add("leftstyle");
    }
    // if(elems){
    //   elem.classList.add("leftstyle");
    // }
  };

  // 改变内容的颜色
  var content = function content() {
    var ele = doc.querySelector('.ant-switch-inner');
    ele.style.fontSize = fontsizes + "px";
    ele.style.color = conentColor;
  };
  var Primaryswitch = function Primaryswitch() {
    return defaultChecked ? /*#__PURE__*/React.createElement(_switch["default"], (0, _extends2["default"])({
      defaultChecked: true
    }, defaultProps, {
      style: _otherProps.style
    })) : /*#__PURE__*/React.createElement(_switch["default"], (0, _extends2["default"])({}, defaultProps, {
      style: _otherProps.style
    }));
  };
  var Buttonswitch = function Buttonswitch() {
    var _useState5 = (0, React.useState)(true),
      disableds = _useState5[0],
      setDisableds = _useState5[1];
    var toggle = function toggle() {
      setDisableds(!disableds);
    };
    return /*#__PURE__*/React.createElement("div", {
      style: _otherProps.style
    }, /*#__PURE__*/React.createElement(_switch["default"], (0, _extends2["default"])({}, defaultProps, {
      disabled: disableds,
      defaultChecked: true,
      style: _otherProps.style
    })), /*#__PURE__*/React.createElement("br", null), "    ", /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement(_button["default"], {
      type: "primary",
      onClick: toggle
    }, buttonname));
  };
  return /*#__PURE__*/React.createElement("div", {
    id: ID,
    style: _styles,
    className: "switchstyle"
  }, /*#__PURE__*/React.createElement(Primaryswitch, null));
};
antSwitch.displayName = 'antSwitch';
var _default = antSwitch;
exports["default"] = _default;