var __assign =
    (this && this.__assign) ||
    function() {
        __assign =
            Object.assign ||
            function(t) {
                for (var s, i = 1, n = arguments.length; i < n; i++) {
                    s = arguments[i];
                    for (var p in s)
                        if (Object.prototype.hasOwnProperty.call(s, p))
                            t[p] = s[p];
                }
                return t;
            };
        return __assign.apply(this, arguments);
    };
import { useEffect, useState } from "react";
import { Dimensions } from "react-native";
var DEFAULT_WIDTH = 375;
var DEFAULT_HEIGHT = 812;
function getDeviceSize(size) {
    return {
        get width() {
            var _a, _b;
            var _c = Dimensions.get("window"),
                width = _c.width,
                height = _c.height;
            return width < height
                ? ((_a = size) === null || _a === void 0 ? void 0 : _a.width) ||
                      DEFAULT_WIDTH
                : ((_b = size) === null || _b === void 0
                      ? void 0
                      : _b.height) || DEFAULT_HEIGHT;
        },
        get height() {
            var _a, _b;
            var _c = Dimensions.get("window"),
                width = _c.width,
                height = _c.height;
            return width < height
                ? ((_a = size) === null || _a === void 0
                      ? void 0
                      : _a.height) || DEFAULT_HEIGHT
                : ((_b = size) === null || _b === void 0 ? void 0 : _b.width) ||
                      DEFAULT_WIDTH;
        }
    };
}
export var useDeviceSize = function(options) {
    var _a, _b;
    var defaultDeviceSize = options.defaultDeviceSize,
        rotate = options.rotate;
    var _c = Dimensions.get("window"),
        width = _c.width,
        height = _c.height;
    var _d = useState({ width: width, height: height }),
        dimensions = _d[0],
        setDimensions = _d[1];
    var _e = useState(getDeviceSize(defaultDeviceSize)),
        baseDeviceSize = _e[0],
        setBaseDeviceSize = _e[1];
    useEffect(
        function() {
            setBaseDeviceSize(getDeviceSize(defaultDeviceSize));
            setDimensions({ width: width, height: height });
            var handleChange = function() {
                var _a = Dimensions.get("window"),
                    changedWidth = _a.width,
                    changedHeight = _a.height;
                setBaseDeviceSize(getDeviceSize(defaultDeviceSize));
                setDimensions({
                    width: changedWidth,
                    height: changedHeight
                });
            };
            if (rotate) {
                Dimensions.addEventListener("change", handleChange);
            }
            return function() {
                if (rotate) {
                    Dimensions.removeEventListener("change", handleChange);
                }
            };
        },
        [
            (_a = options) === null || _a === void 0 ? void 0 : _a.rotate,
            (_b = options) === null || _b === void 0
                ? void 0
                : _b.defaultDeviceSize
        ]
    );
    return __assign(__assign({}, dimensions), {
        baseDeviceSize: baseDeviceSize,
        setBaseDeviceSize: setBaseDeviceSize
    });
};
