"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useScale = exports.parseJwt = exports.useDisclosure = exports.isDesktopSafari = exports.isSafari = exports.getDeviceInfo = exports.isChrome = exports.isMobile = exports.osName = exports.browserName = exports.withAnchorPoint = void 0;
const react_1 = require("react");
const copy_to_clipboard_1 = __importDefault(require("copy-to-clipboard"));
const react_native_base64_1 = __importDefault(require("react-native-base64"));
const react_native_1 = require("react-native");
const { useCallback, useState } = require('react');
const { DyteHelper } = react_native_1.NativeModules;
const ne = new react_native_1.NativeEventEmitter(DyteHelper);
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 };
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 };
};
exports.withAnchorPoint = withAnchorPoint;
const browserName = () => 'react native client';
exports.browserName = browserName;
const osName = () => '';
exports.osName = osName;
const isMobile = () => true;
exports.isMobile = isMobile;
const isChrome = () => true;
exports.isChrome = isChrome;
const getDeviceInfo = () => ({
    isMobile: exports.isMobile(),
    browserName: exports.browserName(),
    osName: exports.osName(),
    browserVersion: 'react native client',
});
exports.getDeviceInfo = getDeviceInfo;
const isSafari = () => false;
exports.isSafari = isSafari;
const isDesktopSafari = () => false;
exports.isDesktopSafari = isDesktopSafari;
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,
    };
};
exports.useDisclosure = useDisclosure;
function useCopyClipboard(text, options) {
    const [isCopied, setIsCopied] = useState(false);
    const successDuration = options && options.successDuration;
    react_1.useEffect(() => {
        if (isCopied && successDuration) {
            const id = setTimeout(() => {
                setIsCopied(false);
            }, successDuration);
            return () => {
                clearTimeout(id);
            };
        }
        return () => { };
    }, [isCopied, successDuration]);
    return [
        isCopied,
        () => {
            const didCopy = copy_to_clipboard_1.default(text);
            setIsCopied(didCopy);
        },
    ];
}
exports.default = useCopyClipboard;
function parseJwt(token) {
    const base64Url = token.split('.')[1];
    const base64S = base64Url.replace(/-/g, '+').replace(/_/g, '/');
    const jsonPayload = decodeURIComponent(react_native_base64_1.default
        .decode(base64S)
        .split('')
        .map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
        .join(''));
    return JSON.parse(jsonPayload);
}
exports.parseJwt = parseJwt;
function useScale({ width, height }) {
    const sWidth = react_native_1.Dimensions.get('screen').width;
    const sheight = react_native_1.Dimensions.get('screen').height;
    return { width: Math.min(width, sWidth), height: Math.min(height, sheight) };
}
exports.useScale = useScale;
//# sourceMappingURL=browser-utils.js.map