import { useEffect } from 'react';
import copy from 'copy-to-clipboard';
import base64 from 'react-native-base64';
import { Dimensions, NativeModules } from 'react-native';
const { useCallback, useState } = require('react');
const { DyteHelper } = NativeModules;
const isValidPointValue = (value) => value >= 0 && value <= 1.0;
const isValidPoint = (point) => isValidPointValue(point.x)
    && isValidPointValue(point.y);
const isValidSize = (size) => size && size.width > 0 && size.height > 0;
const defaultAnchorPoint = { x: 0.5, y: 0.5 };
export const withAnchorPoint = (transform, anchorPoint, size) => {
    if (!isValidPoint(anchorPoint)) {
        return transform;
    }
    if (!isValidSize(size)) {
        return transform;
    }
    let injectedTransform = transform.transform;
    if (!injectedTransform) {
        return transform;
    }
    if (anchorPoint.x !== defaultAnchorPoint.x && size.width) {
        const shiftTranslateX = [];
        // shift before rotation
        shiftTranslateX.push({
            translateX: size.width * (anchorPoint.x - defaultAnchorPoint.x),
        });
        injectedTransform = [...shiftTranslateX, ...injectedTransform];
        // shift after rotation
        injectedTransform.push({
            translateX: size.width * (defaultAnchorPoint.x - anchorPoint.x),
        });
    }
    if (!Array.isArray(injectedTransform)) {
        return { transform: injectedTransform };
    }
    if (anchorPoint.y !== defaultAnchorPoint.y && size.height) {
        const shiftTranslateY = [];
        // shift before rotation
        shiftTranslateY.push({
            translateY: size.height * (anchorPoint.y - defaultAnchorPoint.y),
        });
        injectedTransform = [...shiftTranslateY, ...injectedTransform];
        // shift after rotation
        injectedTransform.push({
            translateY: size.height * (defaultAnchorPoint.y - anchorPoint.y),
        });
    }
    return { transform: injectedTransform };
};
export const browserName = () => 'react native client';
export const osName = () => '';
export const isMobile = () => true;
export const isChrome = () => true;
export const getDeviceInfo = () => ({
    isMobile: isMobile(),
    browserName: browserName(),
    osName: osName(),
    browserVersion: 'react native client',
});
export const isSafari = () => false;
export const isDesktopSafari = () => false;
export const useDisclosure = (isOpenDefault = false) => {
    const [isOpen, setIsOpen] = useState(isOpenDefault);
    const onOpen = useCallback(() => setIsOpen(true), []);
    const onClose = useCallback(() => setIsOpen(false), []);
    const toggle = useCallback(() => setIsOpen((state) => !state), []);
    return {
        isOpen, onOpen, onClose, toggle,
    };
};
export default function useCopyClipboard(text, options) {
    const [isCopied, setIsCopied] = useState(false);
    const successDuration = options && options.successDuration;
    useEffect(() => {
        if (isCopied && successDuration) {
            const id = setTimeout(() => {
                setIsCopied(false);
            }, successDuration);
            return () => {
                clearTimeout(id);
            };
        }
        return () => { };
    }, [isCopied, successDuration]);
    return [
        isCopied,
        () => {
            const didCopy = copy(text);
            setIsCopied(didCopy);
        },
    ];
}
export function parseJwt(token) {
    const base64Url = token.split('.')[1];
    const base64S = base64Url.replace(/-/g, '+').replace(/_/g, '/');
    const jsonPayload = decodeURIComponent(base64.decode(base64S).split('').map((c) => `%${(`00${c.charCodeAt(0).toString(16)}`).slice(-2)}`).join(''));
    return JSON.parse(jsonPayload);
}
/**
 * Returns true if the screen is in portrait mode
 */
const isPortrait = () => {
    const dim = Dimensions.get('screen');
    return dim.height >= dim.width;
};
/**
 * A React Hook which updates when the orientation changes
 * @returns whether the user is in 'PORTRAIT' or 'LANDSCAPE'
 */
export function useOrientation() {
    // State to hold the connection status
    const [orientation, setOrientation] = useState(isPortrait() ? 'PORTRAIT' : 'LANDSCAPE');
    const [forced, setForced] = useState(false);
    useEffect(() => {
        const callback = () => {
            setOrientation(isPortrait() ? 'PORTRAIT' : 'LANDSCAPE');
        };
        Dimensions.addEventListener('change', callback);
        const getOrientation = async () => {
            const isForced = await DyteHelper.isForcedLandscape();
            setForced(isForced || false);
        };
        getOrientation();
        return () => {
            Dimensions.removeEventListener('change', callback);
        };
    }, []);
    return { orientation, forced, setForced };
}
//# sourceMappingURL=browser-utils.js.map