import _extends from "@babel/runtime/helpers/extends";
import { ConditionRenderService } from "lowcode/_utils/conditionRenderService";
import React, { useState, useEffect } from "react";
import { isBoolean } from "lodash";
import { hiddenBoxType } from "./type";

/**
 * 需要分析出依赖在做监听
 * @param props 
 * @returns 
 */
var HiddenBox = function HiddenBox(props) {
  var conditions = props.conditions,
    hidden = props.hidden,
    show = props.show,
    destroyOnClose = props.destroyOnClose;
  var _useState = useState(initValue(conditions)),
    value = _useState[0],
    setValue = _useState[1];
  useEffect(function () {
    if (props.__designMode !== "design") {
      getHiddenData();
    }
  }, [props]);
  function getHiddenData() {
    if (isDefault()) return;
    var conditionData = conditions === hiddenBoxType.hidden ? hidden : show;
    if (typeof conditionData === "boolean") {
      handleSetValue(conditionData);
    }
    if (conditionData.data) {
      var conditionRenderService = new ConditionRenderService();
      conditionRenderService.load(conditionData.data);
      conditionRenderService.loadProps(props);
      var res = conditionRenderService.getComputedResult();
      res = isBoolean(res) ? res : false;
      handleSetValue(res);
    }
  }
  function isDefault() {
    return conditions !== hiddenBoxType.hidden && conditions !== hiddenBoxType.show;
  }

  // 如果配置了显示，则初始化为隐藏，如果配置了隐藏，则初始为显示
  function initValue(conditions) {
    if (isDefault() || props.__designMode === "design") return false;
    return !(conditions === hiddenBoxType.hidden);
  }
  function handleSetValue(result) {
    // 隐藏则直接使用结果，显示则取反
    var resultRes = conditions === hiddenBoxType.hidden ? result : !result;
    // 不对等的情况下才做处理
    if (resultRes !== value) {
      setValue(resultRes);
    }
  }

  // 销毁或者销毁不存在兼容之前的，则直接销毁
  if (destroyOnClose || destroyOnClose === undefined) {
    return /*#__PURE__*/React.createElement("div", {
      style: props.style,
      className: "HiddenBox"
    }, value ? null : props.children);
  } else {
    return /*#__PURE__*/React.createElement("div", {
      style: _extends({}, props.style, {
        display: value ? 'none' : 'block'
      }),
      className: "HiddenBox"
    }, props.children);
  }
};
export default HiddenBox;