"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModalPrimitive = exports.ModalClose = exports.ModalFooter = exports.ModalBody = exports.ModalHeader = exports.ModalContent = exports.ModalOverlay = exports.ModalRoot = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const react_native_1 = require("react-native");
const theme_1 = require("../../../lib/theme/theme");
const { width: screenWidth, height: screenHeight } = react_native_1.Dimensions.get("window");
// Modal root primitive - handles the native Modal component
exports.ModalRoot = (0, react_1.forwardRef)(({ open = false, onOpenChange, children, onRequestClose, animationType = "fade", presentationStyle = react_native_1.Platform.OS === "ios" ? "pageSheet" : "formSheet", statusBarTranslucent = react_native_1.Platform.OS !== "ios", transparent = true, ...props }, ref) => {
    const handleRequestClose = react_1.default.useCallback((e) => {
        if (onOpenChange) {
            onOpenChange(false);
        }
        if (onRequestClose) {
            onRequestClose(e);
        }
    }, [onOpenChange, onRequestClose]);
    return ((0, jsx_runtime_1.jsx)(react_native_1.Modal, { visible: open, onRequestClose: handleRequestClose, animationType: animationType, presentationStyle: presentationStyle, statusBarTranslucent: statusBarTranslucent, transparent: transparent, ...props, children: (0, jsx_runtime_1.jsx)(react_native_1.View, { ref: ref, style: primitiveStyles.container, children: children }) }));
});
exports.ModalRoot.displayName = "ModalRoot";
exports.ModalOverlay = (0, react_1.forwardRef)(({ dismissible = true, onDismiss, onPress, style, children, activeOpacity = 1, ...props }, ref) => {
    const handlePress = react_1.default.useCallback((event) => {
        if (dismissible && onDismiss) {
            onDismiss();
        }
        if (onPress) {
            onPress(event);
        }
    }, [dismissible, onDismiss, onPress]);
    return ((0, jsx_runtime_1.jsx)(react_native_1.TouchableOpacity, { ref: ref, style: [primitiveStyles.overlay, style], activeOpacity: activeOpacity, onPress: handlePress, ...props, children: children }));
});
exports.ModalOverlay.displayName = "ModalOverlay";
exports.ModalContent = (0, react_1.forwardRef)(({ children, position = "center", size = "md", style, onStartShouldSetResponder, ...props }, ref) => {
    // Prevent touches from propagating to overlay
    const handleStartShouldSetResponder = react_1.default.useCallback(() => {
        return true;
    }, []);
    const positionStyle = react_1.default.useMemo(() => {
        switch (position) {
            case "top":
                return primitiveStyles.contentTop;
            case "bottom":
                return primitiveStyles.contentBottom;
            case "left":
                return primitiveStyles.contentLeft;
            case "right":
                return primitiveStyles.contentRight;
            case "center":
            default:
                return primitiveStyles.contentCenter;
        }
    }, [position]);
    const sizeStyle = react_1.default.useMemo(() => {
        switch (size) {
            case "sm":
                return primitiveStyles.sizeSm;
            case "lg":
                return primitiveStyles.sizeLg;
            case "xl":
                return primitiveStyles.sizeXl;
            case "full":
                return primitiveStyles.sizeFull;
            case "md":
            default:
                return primitiveStyles.sizeMd;
        }
    }, [size]);
    return ((0, jsx_runtime_1.jsx)(react_native_1.View, { ref: ref, style: [primitiveStyles.content, positionStyle, sizeStyle, style], onStartShouldSetResponder: onStartShouldSetResponder || handleStartShouldSetResponder, ...props, children: children }));
});
exports.ModalContent.displayName = "ModalContent";
exports.ModalHeader = (0, react_1.forwardRef)(({ children, withBorder = false, style, ...props }, ref) => {
    const { theme } = (0, theme_1.useTheme)();
    const borderStyle = withBorder
        ? { borderBottomWidth: 1, borderBottomColor: theme.colors.border }
        : null;
    return ((0, jsx_runtime_1.jsx)(react_native_1.View, { ref: ref, style: [primitiveStyles.header, borderStyle, style], ...props, children: children }));
});
exports.ModalHeader.displayName = "ModalHeader";
exports.ModalBody = (0, react_1.forwardRef)(({ children, scrollable = true, style, ...props }, ref) => {
    if (!scrollable) {
        return (0, jsx_runtime_1.jsx)(react_native_1.View, { style: [primitiveStyles.body, style], children: children });
    }
    return ((0, jsx_runtime_1.jsx)(react_native_1.ScrollView, { ref: ref, style: [primitiveStyles.body, style], showsVerticalScrollIndicator: false, keyboardShouldPersistTaps: "handled", ...props, children: children }));
});
exports.ModalBody.displayName = "ModalBody";
exports.ModalFooter = (0, react_1.forwardRef)(({ children, withBorder = false, direction = "row", justify = "flex-end", style, ...props }, ref) => {
    const { theme } = (0, theme_1.useTheme)();
    const borderStyle = withBorder
        ? { borderTopWidth: 1, borderTopColor: theme.colors.border }
        : null;
    return ((0, jsx_runtime_1.jsx)(react_native_1.View, { ref: ref, style: [
            primitiveStyles.footer,
            borderStyle,
            {
                flexDirection: direction,
                justifyContent: justify,
            },
            style,
        ], ...props, children: children }));
});
exports.ModalFooter.displayName = "ModalFooter";
exports.ModalClose = (0, react_1.forwardRef)(({ children, onClose, onPress, ...props }, ref) => {
    const handlePress = react_1.default.useCallback((event) => {
        if (onClose) {
            onClose();
        }
        if (onPress) {
            onPress(event);
        }
    }, [onClose, onPress]);
    return ((0, jsx_runtime_1.jsx)(react_native_1.TouchableOpacity, { ref: ref, onPress: handlePress, ...props, children: children }));
});
exports.ModalClose.displayName = "ModalClose";
// Primitive styles (minimal, unstyled)
const primitiveStyles = react_native_1.StyleSheet.create({
    container: {
        flex: 1,
    },
    overlay: {
        flex: 1,
        backgroundColor: "rgba(0, 0, 0, 0.5)",
        justifyContent: "center",
        alignItems: "center",
        padding: 16,
    },
    content: {
        borderRadius: 8,
        overflow: "hidden",
    },
    contentCenter: {
        alignSelf: "center",
    },
    contentTop: {
        alignSelf: "center",
        marginTop: 0,
    },
    contentBottom: {
        alignSelf: "center",
        marginTop: "auto",
    },
    contentLeft: {
        alignSelf: "flex-start",
        marginRight: "auto",
    },
    contentRight: {
        alignSelf: "flex-end",
        marginLeft: "auto",
    },
    sizeSm: {
        maxWidth: screenWidth * 0.4,
        maxHeight: screenHeight * 0.6,
    },
    sizeMd: {
        maxWidth: screenWidth * 0.6,
        maxHeight: screenHeight * 0.8,
    },
    sizeLg: {
        maxWidth: screenWidth * 0.8,
        maxHeight: screenHeight * 0.9,
    },
    sizeXl: {
        maxWidth: screenWidth * 0.95,
        maxHeight: screenHeight * 0.95,
    },
    sizeFull: {
        width: screenWidth,
        height: screenHeight,
        maxWidth: screenWidth,
        maxHeight: screenHeight,
        borderRadius: 0,
    },
    header: {
        paddingHorizontal: 16,
        paddingVertical: 12,
        flexDirection: "row",
        alignItems: "center",
        justifyContent: "space-between",
    },
    body: {
        flex: 1,
        paddingHorizontal: 16,
        paddingVertical: 12,
    },
    footer: {
        paddingHorizontal: 16,
        paddingVertical: 12,
        flexDirection: "row",
        alignItems: "center",
        gap: 8,
    },
});
// Export primitive collection
exports.ModalPrimitive = {
    Root: exports.ModalRoot,
    Overlay: exports.ModalOverlay,
    Content: exports.ModalContent,
    Header: exports.ModalHeader,
    Body: exports.ModalBody,
    Footer: exports.ModalFooter,
    Close: exports.ModalClose,
};
//# sourceMappingURL=modal.js.map