function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useState } from 'react';
import classNames from 'classnames';
import useEngineContext from "../../hooks/useEngineContext";
import "./index.less";
var PREFIX_CLS = 'pisell-safe-area-top';
var getBodyScaleY = function getBodyScaleY() {
  if (typeof window === 'undefined' || typeof document === 'undefined') {
    return 1;
  }
  var transform = window.getComputedStyle(document.body).transform;
  if (!transform || transform === 'none') {
    return 1;
  }
  var matrix = new DOMMatrixReadOnly(transform);
  return Math.sqrt(matrix.c * matrix.c + matrix.d * matrix.d) || 1;
};
var useBodyScaleY = function useBodyScaleY(enabled) {
  var _useState = useState(getBodyScaleY),
    _useState2 = _slicedToArray(_useState, 2),
    bodyScaleY = _useState2[0],
    setBodyScaleY = _useState2[1];
  useEffect(function () {
    var _window$visualViewpor;
    if (!enabled || typeof window === 'undefined' || typeof document === 'undefined') {
      return undefined;
    }
    var updateBodyScaleY = function updateBodyScaleY() {
      setBodyScaleY(getBodyScaleY());
    };
    updateBodyScaleY();
    window.addEventListener('resize', updateBodyScaleY);
    (_window$visualViewpor = window.visualViewport) === null || _window$visualViewpor === void 0 || _window$visualViewpor.addEventListener('resize', updateBodyScaleY);
    var bodyObserver = new MutationObserver(updateBodyScaleY);
    bodyObserver.observe(document.body, {
      attributes: true,
      attributeFilter: ['style', 'class']
    });
    return function () {
      var _window$visualViewpor2;
      window.removeEventListener('resize', updateBodyScaleY);
      (_window$visualViewpor2 = window.visualViewport) === null || _window$visualViewpor2 === void 0 || _window$visualViewpor2.removeEventListener('resize', updateBodyScaleY);
      bodyObserver.disconnect();
    };
  }, [enabled]);
  return enabled ? bodyScaleY : 1;
};
var getSafeAreaTopInset = function getSafeAreaTopInset() {
  if (typeof window === 'undefined' || typeof document === 'undefined') {
    return 0;
  }
  var probe = document.createElement('div');
  probe.style.cssText = ['position:absolute', 'visibility:hidden', 'pointer-events:none', 'height:constant(safe-area-inset-top)', 'height:env(safe-area-inset-top)'].join(';');
  document.body.appendChild(probe);
  var height = Number.parseFloat(window.getComputedStyle(probe).height) || 0;
  document.body.removeChild(probe);
  return height;
};
var useSafeAreaTopInset = function useSafeAreaTopInset(enabled) {
  var _useState3 = useState(0),
    _useState4 = _slicedToArray(_useState3, 2),
    safeAreaTopInset = _useState4[0],
    setSafeAreaTopInset = _useState4[1];
  useEffect(function () {
    var _window$visualViewpor3;
    if (!enabled || typeof window === 'undefined' || typeof document === 'undefined') {
      setSafeAreaTopInset(0);
      return undefined;
    }
    var updateSafeAreaTopInset = function updateSafeAreaTopInset() {
      setSafeAreaTopInset(getSafeAreaTopInset());
    };
    updateSafeAreaTopInset();
    window.addEventListener('resize', updateSafeAreaTopInset);
    (_window$visualViewpor3 = window.visualViewport) === null || _window$visualViewpor3 === void 0 || _window$visualViewpor3.addEventListener('resize', updateSafeAreaTopInset);
    return function () {
      var _window$visualViewpor4;
      window.removeEventListener('resize', updateSafeAreaTopInset);
      (_window$visualViewpor4 = window.visualViewport) === null || _window$visualViewpor4 === void 0 || _window$visualViewpor4.removeEventListener('resize', updateSafeAreaTopInset);
    };
  }, [enabled]);
  return enabled ? safeAreaTopInset : 0;
};
var normalizeCssLength = function normalizeCssLength(value) {
  if (typeof value === 'number') {
    return "".concat(value, "px");
  }
  return value;
};

/**
 * SafeAreaTop 占据 WebView 顶部安全区，并允许页面独立设置该区域背景色。
 */
var NativeSafeAreaTop = /*#__PURE__*/React.memo(function (props) {
  var _props$backgroundColo = props.backgroundColor,
    backgroundColor = _props$backgroundColo === void 0 ? 'transparent' : _props$backgroundColo,
    className = props.className,
    style = props.style;
  var bodyScaleY = useBodyScaleY(true);
  var safeAreaTopInset = useSafeAreaTopInset(true);
  var safeAreaTopHeight = "".concat(safeAreaTopInset / bodyScaleY, "px");
  var styleHeight = normalizeCssLength(style === null || style === void 0 ? void 0 : style.height);
  var mergedStyle = _objectSpread({
    '--safe-area-top-background': backgroundColor,
    '--safe-area-top-height': styleHeight || safeAreaTopHeight
  }, style);
  return /*#__PURE__*/React.createElement("div", {
    "aria-hidden": "true",
    className: classNames(PREFIX_CLS, className),
    style: mergedStyle
  });
});
NativeSafeAreaTop.displayName = 'NativeSafeAreaTop';
export var SafeAreaTop = /*#__PURE__*/React.memo(function (props) {
  var _context$appHelper;
  var context = useEngineContext();
  var _ref = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {},
    isNativePlatform = _ref.isNativePlatform;
  var isNative = typeof isNativePlatform === 'function' ? isNativePlatform() : isNativePlatform || false;
  if (!isNative) {
    return /*#__PURE__*/React.createElement("div", {
      className: classNames(PREFIX_CLS, props.className),
      style: {
        display: 'none'
      }
    });
  }
  return /*#__PURE__*/React.createElement(NativeSafeAreaTop, props);
});
SafeAreaTop.displayName = 'SafeAreaTop';
export default SafeAreaTop;