import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/taggedTemplateLiteralLoose";
var _excluded = ["dictionaryKey"];
var _templateObject, _templateObject2;
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { PureComponent, useEffect, useState } from "react";
import { CLowCodeService } from "../../utils/CLowCodeService";
import styled, { css } from "styled-components";
import { getStyleByProps } from "../../utils/comfn/styleFn";
import { handleDictionaryData } from "../../utils/comfn/getLangByDictionary";
import { isEffectiveEventFlow, showTextData, subScribeCom } from "../../utils/comfn/util";
import { ComponentStyle } from "../../utils/type";
import ButtonInner from "./button";
import "./index.less";
var StyledButton = styled(ButtonInner)(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["\n  ", "\n"])), function (props) {
  return css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose(["\n    ", "\n  "])), getStyleByProps(props));
});
var Button = function Button(props) {
  var _props$href;
  var _props = Object.assign({}, props || {});
  var cLowCodeService = new CLowCodeService();
  var _useState = useState([]),
    beforeUrlList = _useState[0],
    setBeforeUrlList = _useState[1];
  var _useState2 = useState(props.children),
    newChildren = _useState2[0],
    setNewChildren = _useState2[1];

  // 用于选择状态时切换 props.selectStyle样式
  var _useState3 = useState(props.style || {}),
    newStyle = _useState3[0],
    setNewStyle = _useState3[1];
  var _useState4 = useState(false),
    isSelect = _useState4[0],
    setIsSelect = _useState4[1];
  if (!((_props$href = props.href) !== null && _props$href !== void 0 && _props$href.trim()) || props.__designMode === "design") {
    // 解决低代码编辑器中按钮设置href属性造成按钮点击重定向问题
    _props.href = undefined;
  }
  delete _props.icon;
  useEffect(function () {
    if (props.__designMode !== "design" && props.componentKey) {
      // 将点击和hover的资源背景图片提前加载好
      handleClickOrHoverImgUrl();
      if (props.loopData) {
        props.loopData.subScribeComponentCallback(props.componentKey, props.loopData, handleSubScribeOwn);
      } else {
        return subScribeCom({
          code: props.componentKey,
          unsubscribeOwn: false,
          webGid: props.webGid,
          handleSubScribeOwn: handleSubScribeOwn
        });
      }
    } else {
      var _newStyle$dictionaryK;
      if ((_newStyle$dictionaryK = newStyle.dictionaryKey) !== null && _newStyle$dictionaryK !== void 0 && _newStyle$dictionaryK.length) {
        var dictionaryKey = newStyle.dictionaryKey,
          other = _objectWithoutPropertiesLoose(newStyle, _excluded);
        setNewStyle(other);
      }
    }
  }, []);
  useEffect(function () {
    if (props.__designMode === "design") {
      setNewStyle(props.style);
    }
  }, [props.style]);
  useEffect(function () {
    var str = handleDictionaryData(props.children, props.dictionaryKey, props.webGid);
    if (str !== null) {
      var res = showTextData(str);
      if (res.show) {
        str = res.text;
      }
    }
    setNewChildren(str);
  }, [props.children, props.dictionaryKey]);
  var handleClickOrHoverImgUrl = function handleClickOrHoverImgUrl() {
    var _props$clickStyle, _props$clickStyle$bac, _props$hoverStyle, _props$hoverStyle$bac;
    var result = [];
    if ((_props$clickStyle = props.clickStyle) !== null && _props$clickStyle !== void 0 && (_props$clickStyle$bac = _props$clickStyle.backgroundImage) !== null && _props$clickStyle$bac !== void 0 && _props$clickStyle$bac.length) {
      result.push(props.clickStyle.backgroundImage);
    }
    if ((_props$hoverStyle = props.hoverStyle) !== null && _props$hoverStyle !== void 0 && (_props$hoverStyle$bac = _props$hoverStyle.backgroundImage) !== null && _props$hoverStyle$bac !== void 0 && _props$hoverStyle$bac.length) {
      result.push(props.hoverStyle.backgroundImage);
    }
    setBeforeUrlList(result);
  };
  var handleSubScribeOwn = function handleSubScribeOwn(data) {
    var _data$params;
    if ((_data$params = data.params) !== null && _data$params !== void 0 && _data$params.styleType) {
      // 默认样式特殊处理
      var style = props.style;
      var newIsSelect = false;
      if (data.params.styleType === ComponentStyle.selectStyle) {
        newIsSelect = true;
      }

      // 其他样式会优先继承默认样式
      if (data.params.styleType !== ComponentStyle.defaultStyle) {
        var otherStyle = props[data.params.styleType];
        style = _extends({}, style, otherStyle);
      }
      setIsSelect(newIsSelect);
      setNewStyle(style || {});
    }
  };
  var handleOnClick = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(e) {
      var _props$loopData, _props$loopData2;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            if (!(props.__designMode !== "design")) {
              _context.next = 6;
              break;
            }
            // 优先处理循环选中
            if ((_props$loopData = props.loopData) !== null && _props$loopData !== void 0 && _props$loopData.publishComponentCallback) {
              (_props$loopData2 = props.loopData) === null || _props$loopData2 === void 0 ? void 0 : _props$loopData2.publishComponentCallback({
                selected: !isSelect,
                loopData: props.loopData
              });
            }
            // 在处理自身的事件流
            if (!isEffectiveEventFlow(props.flowEventConfig)) {
              _context.next = 6;
              break;
            }
            cLowCodeService.loadPageProps(props);
            _context.next = 6;
            return cLowCodeService.handleFlowEvent();
          case 6:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function handleOnClick(_x) {
      return _ref.apply(this, arguments);
    };
  }();

  // 直接在这里处理按钮事件
  return /*#__PURE__*/React.createElement(React.Fragment, null, beforeUrlList.map(function (url) {
    return /*#__PURE__*/React.createElement("img", {
      style: {
        backgroundImage: "" + url,
        width: "0px",
        height: "0px"
      }
    });
  }), /*#__PURE__*/React.createElement(StyledButton, _extends({}, props, _props, {
    isSelect: isSelect,
    onClick: handleOnClick,
    style: newStyle,
    children: newChildren
  })));
};
var _default = /*#__PURE__*/function (_PureComponent) {
  function _default() {
    return _PureComponent.apply(this, arguments) || this;
  }
  _inheritsLoose(_default, _PureComponent);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/React.createElement(Button, _extends({
      className: "buttonInner"
    }, this.props));
  };
  return _default;
}(PureComponent);
export { _default as default };