import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "core-js/modules/es.array.concat.js";
import "core-js/modules/web.timers.js";
import { useEffect, useRef, useState } from 'react';

/**
 * 最小化
 * Minimize
 * @returns
 */
export var useMinimize = function useMinimize(_ref) {
  var minimize = _ref.minimize,
    _ref$afterChange = _ref.afterChange,
    afterChange = _ref$afterChange === void 0 ? function () {} : _ref$afterChange,
    _ref$beforeChange = _ref.beforeChange,
    beforeChange = _ref$beforeChange === void 0 ? function () {} : _ref$beforeChange;
  var ref = useRef(null);
  var _useState = useState({
      transform: "translate3d(0px, 0px, 0) scale(1)"
    }),
    _useState2 = _slicedToArray(_useState, 2),
    style = _useState2[0],
    setStyle = _useState2[1];
  useEffect(function () {
    var timer = 0;
    beforeChange === null || beforeChange === void 0 || beforeChange(minimize);
    if (minimize) {
      var _document$querySelect, _ref$current;
      var targetEleBounds = (_document$querySelect = document.querySelector('.fcr-status-bar-widget-slot')) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.getBoundingClientRect();
      var selfEleBounds = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.getBoundingClientRect();
      if (targetEleBounds && selfEleBounds) {
        var x = targetEleBounds.left - selfEleBounds.left - selfEleBounds.width / 2;
        var y = targetEleBounds.top - selfEleBounds.top - selfEleBounds.height / 2;
        setTimeout(function () {
          setStyle({
            transform: "translate3d(".concat(x, "px, ").concat(y, "px, 0) scale(0)"),
            transition: 'all 0.5s'
          });
          timer = window.setTimeout(function () {
            afterChange === null || afterChange === void 0 || afterChange(minimize);
            setStyle({
              transform: "translate3d(".concat(x, "px, ").concat(y, "px, 0) scale(0)")
            });
          }, 500);
        });
      }
    } else {
      setTimeout(function () {
        setStyle({
          transform: "translate3d(0px, 0px, 0) scale(1)",
          transition: 'all 0.5s'
        });
        setTimeout(function () {
          afterChange === null || afterChange === void 0 || afterChange(minimize);
          setStyle({
            transform: "translate3d(0px, 0px, 0) scale(1)"
          });
        }, 500);
      });
    }
    return function () {
      window.clearTimeout(timer);
    };
  }, [minimize]);
  return {
    ref: ref,
    style: style
  };
};