"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollaborativeSection = exports.useCollaborationContext = exports.CollaborationProvider = void 0;
exports.withCollaboration = withCollaboration;
const react_1 = __importStar(require("react"));
const useCollaboration_1 = require("../client/useCollaboration");
const CollaborationContext = (0, react_1.createContext)(null);
/**
 * Collaboration Provider Component
 * Provides collaboration context to child components
 */
const CollaborationProvider = ({ children, options }) => {
    const collaboration = (0, useCollaboration_1.useCollaboration)(options);
    const [awareness, setAwareness] = (0, react_1.useState)(new Map());
    // Track user awareness updates
    (0, react_1.useEffect)(() => {
        if (options.onAwarenessUpdate) {
            const originalHandler = options.onAwarenessUpdate;
            options.onAwarenessUpdate = (userId, awarenessData) => {
                setAwareness(prev => {
                    const updated = new Map(prev);
                    updated.set(userId, awarenessData);
                    return updated;
                });
                originalHandler(userId, awarenessData);
            };
        }
    }, []);
    // Broadcast section awareness when user navigates
    const updateSectionAwareness = (section, activity) => {
        collaboration.updateAwareness({
            section,
            activity,
            timestamp: new Date().toISOString()
        });
    };
    // Enhanced context value with awareness helpers
    const contextValue = {
        ...collaboration,
        documentId: options.documentId,
        documentType: options.documentType
    };
    return (<CollaborationContext.Provider value={contextValue}>
      {children}
    </CollaborationContext.Provider>);
};
exports.CollaborationProvider = CollaborationProvider;
/**
 * Hook to use collaboration context
 */
const useCollaborationContext = () => {
    const context = (0, react_1.useContext)(CollaborationContext);
    if (!context) {
        throw new Error('useCollaborationContext must be used within CollaborationProvider');
    }
    return context;
};
exports.useCollaborationContext = useCollaborationContext;
/**
 * HOC to inject collaboration props
 */
function withCollaboration(Component) {
    return (props) => {
        const collaboration = (0, exports.useCollaborationContext)();
        return <Component {...props} {...collaboration}/>;
    };
}
const CollaborativeSection = ({ sectionId, children, trackActivity = true, className = '' }) => {
    const { updateAwareness } = (0, exports.useCollaborationContext)();
    const [isInView, setIsInView] = (0, react_1.useState)(false);
    (0, react_1.useEffect)(() => {
        if (!trackActivity)
            return;
        const observer = new IntersectionObserver(([entry]) => {
            setIsInView(entry.isIntersecting);
            if (entry.isIntersecting) {
                updateAwareness({
                    section: sectionId,
                    activity: 'viewing',
                    timestamp: new Date().toISOString()
                });
            }
        }, { threshold: 0.5 });
        const element = document.getElementById(sectionId);
        if (element) {
            observer.observe(element);
        }
        return () => {
            if (element) {
                observer.unobserve(element);
            }
        };
    }, [sectionId, trackActivity, updateAwareness]);
    return (<div id={sectionId} className={`collaborative-section ${className} ${isInView ? 'in-view' : ''}`} data-section={sectionId}>
      {children}
    </div>);
};
exports.CollaborativeSection = CollaborativeSection;
//# sourceMappingURL=CollaborationProvider.js.map