"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _switch = _interopRequireDefault(require("@alifd/next/lib/switch"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = require("react");
require("./index.less");
var SlotSetter = exports["default"] = /*#__PURE__*/function (_Component) {
  function SlotSetter() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _Component.call.apply(_Component, [this].concat(args)) || this;
    _this.onChangeSwitch = function (checked) {
      var onChange = _this.props.onChange;
      if (checked) {
        _this.handleInitial();
      } else {
        onChange && onChange(null);
      }
    };
    _this.handleInitial = function () {
      var _this$props = _this.props,
        onChange = _this$props.onChange,
        onInitial = _this$props.onInitial;
      if (onInitial) {
        onInitial();
        return;
      }
      if (!onChange) {
        return;
      }
      onChange({
        type: 'JSSlot',
        value: null
      });
    };
    // 模板选择事件
    _this.onTemplateChange = function (value) {
      var _this$props2 = _this.props,
        onChange = _this$props2.onChange,
        templates = _this$props2.templates;
      if (value === 'jsslot') {
        _this.handleInitial();
      } else if (value === 'disable') {
        onChange && onChange(null);
      } else {
        var template = templates.find(function (item) {
          return item.value === value;
        });
        if (template) {
          onChange && onChange((0, _extends2["default"])({}, template.content, {
            name: template.value
          }));
        }
      }
    };
    _this.slotIsOpen = function (initialValue) {
      if (initialValue) {
        var value = initialValue.value,
          visible = initialValue.visible;
        if (value) {
          if (visible == undefined) {
            if (Array.isArray(value) && value.length == 0) {
              return false;
            } else if ((value === null || value === void 0 ? void 0 : value.length) > 0) {
              return true;
            }
          } else {
            return visible;
          }
        }
      }
      return false;
    };
    return _this;
  }
  (0, _inheritsLoose2["default"])(SlotSetter, _Component);
  var _proto = SlotSetter.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var _this$props3 = this.props,
      value = _this$props3.value,
      _onChange = _this$props3.onChange,
      supportParams = _this$props3.supportParams,
      hideParams = _this$props3.hideParams,
      _this$props3$checkedT = _this$props3.checkedText,
      checkedText = _this$props3$checkedT === void 0 ? '启用' : _this$props3$checkedT,
      _this$props3$unChecke = _this$props3.unCheckedText,
      unCheckedText = _this$props3$unChecke === void 0 ? '关闭' : _this$props3$unChecke,
      templates = _this$props3.templates;
    var isOpenSlot = this.slotIsOpen(value);
    var switchComponent = null;
    if (templates) {
      // 模板场景下，使用下拉列表切换
      var templateName = (value === null || value === void 0 ? void 0 : value.name) || (isOpenSlot ? 'jsslot' : 'disable');
      switchComponent = /*#__PURE__*/React.createElement(_select["default"], {
        dataSource: templates,
        defaultValue: templateName,
        onChange: this.onTemplateChange,
        autoWidth: false
      });
    } else {
      // 标准场景下，使用开关进行切换
      switchComponent = /*#__PURE__*/React.createElement(_switch["default"], {
        autoWidth: true,
        checked: !!value,
        defaultChecked: isOpenSlot,
        onChange: function onChange(checked) {
          return _this2.onChangeSwitch(checked);
        },
        size: "small",
        checkedChildren: checkedText,
        unCheckedChildren: unCheckedText
      });
    }
    var hasParams = value && value.params && Array.isArray(value.params);
    return /*#__PURE__*/React.createElement("div", {
      className: "lc-setter-slot lc-setter-slot-column"
    }, switchComponent, !hideParams && hasParams ? /*#__PURE__*/React.createElement(_input["default"], {
      className: "lc-slot-params",
      addonTextBefore: "\u5165\u53C2",
      placeholder: "\u63D2\u69FD\u5165\u53C2\uFF0C\u4EE5\u9017\u53F7\u5206\u9694",
      value: value.params.join(','),
      onChange: function onChange(val) {
        val = val.trim();
        var params = val ? val.split(/ *, */) : [];
        _onChange && _onChange((0, _extends2["default"])({}, value, {
          params: params.length == 0 ? [''] : params
        }));
      },
      addonAfter: /*#__PURE__*/React.createElement(_button["default"], {
        type: "secondary",
        onClick: function onClick() {
          _onChange && _onChange((0, _extends2["default"])({}, value, {
            params: ['']
          }));
        }
      }, /*#__PURE__*/React.createElement(_icon["default"], {
        type: "close"
      }))
    }) : supportParams ? /*#__PURE__*/React.createElement(_button["default"], {
      className: "lc-slot-params",
      type: "primary",
      onClick: function onClick() {
        _onChange && _onChange((0, _extends2["default"])({}, value, {
          params: []
        }));
      }
    }, "\u6DFB\u52A0\u5165\u53C2") : null);
  };
  return SlotSetter;
}(_react.Component);