"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const useIsomorphicEffect = require("../use-isomorphic-effect/use-isomorphic-effect.cjs");
function getInitialValue(initialValue, getInitialValueInEffect) {
  var _a, _b;
  if (getInitialValueInEffect) {
    return initialValue;
  }
  if (typeof window !== "undefined" && "screen" in window) {
    return {
      angle: ((_a = window.screen.orientation) == null ? void 0 : _a.angle) ?? initialValue.angle,
      type: ((_b = window.screen.orientation) == null ? void 0 : _b.type) ?? initialValue.type
    };
  }
  return initialValue;
}
function useOrientation({
  defaultAngle = 0,
  defaultType = "landscape-primary",
  getInitialValueInEffect = true
} = {}) {
  const [orientation, setOrientation] = React.useState(
    getInitialValue(
      {
        angle: defaultAngle,
        type: defaultType
      },
      getInitialValueInEffect
    )
  );
  const handleOrientationChange = (event) => {
    const target = event.currentTarget;
    setOrientation({ angle: (target == null ? void 0 : target.angle) || 0, type: (target == null ? void 0 : target.type) || "landscape-primary" });
  };
  useIsomorphicEffect.useIsomorphicEffect(() => {
    var _a;
    (_a = window.screen.orientation) == null ? void 0 : _a.addEventListener("change", handleOrientationChange);
    return () => {
      var _a2;
      return (_a2 = window.screen.orientation) == null ? void 0 : _a2.removeEventListener("change", handleOrientationChange);
    };
  }, []);
  return orientation;
}
exports.useOrientation = useOrientation;
