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 { CommentDialog } from '../../CommentDialog/CommentDialog';
import React, { createContext, useContext, useState, useRef, useCallback } from 'react';
var CommentDialogContext = createContext(undefined);
export var useCommentDialog = function () {
    var context = useContext(CommentDialogContext);
    if (!context)
        throw new Error('useCommentDialog must be used within CommentDialogProvider');
    return context;
};
export var CommentDialogProvider = function (_a) {
    var children = _a.children;
    var _b = useState(null), options = _b[0], setOptions = _b[1];
    var commentResolverRef = useRef(null);
    var showCommentDialog = useCallback(function (options) {
        setOptions(options || {});
        return new Promise(function (resolve) {
            commentResolverRef.current = resolve;
        });
    }, []);
    var handleClose = function (result) {
        var _a;
        setOptions(null);
        (_a = commentResolverRef.current) === null || _a === void 0 ? void 0 : _a.call(commentResolverRef, result);
        commentResolverRef.current = null;
    };
    return (React.createElement(CommentDialogContext.Provider, { value: showCommentDialog },
        children,
        options && React.createElement(CommentDialog, __assign({}, options, { onConfirm: handleClose, onCancel: function () { return handleClose(null); } }))));
};
