"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScrollToElementProvider = exports.ScrollToElementContext = exports.isHighlightedAttributeType = exports.isHighlightedErrorType = exports.ScrollType = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var ScrollType;
(function (ScrollType) {
    ScrollType["Error"] = "Error";
    ScrollType["NewAttribute"] = "NewAttribute";
    ScrollType["NewView"] = "NewView";
})(ScrollType || (exports.ScrollType = ScrollType = {}));
var isHighlightedErrorType = function (type) { return type === ScrollType.Error; };
exports.isHighlightedErrorType = isHighlightedErrorType;
var isHighlightedAttributeType = function (type) { return type === ScrollType.NewAttribute; };
exports.isHighlightedAttributeType = isHighlightedAttributeType;
exports.ScrollToElementContext = react_1.default.createContext({
    element: null,
    type: null,
    highlightView: ramda_1.identity,
    highlightError: ramda_1.identity,
    highlightAttribute: ramda_1.identity,
    scrollIntoRef: ramda_1.identity
});
var ScrollToElementProvider = function (_a) {
    var children = _a.children;
    var _b = (0, react_1.useState)(null), element = _b[0], setElement = _b[1];
    var highlightTimer = (0, react_1.useRef)(null);
    var scrollIntoRef = (0, react_1.useCallback)(function (ref) {
        if (!ref.current)
            return;
        ref.current.scrollIntoView({ behavior: 'smooth', block: 'center' });
        clearTimeout(highlightTimer.current);
        highlightTimer.current = setTimeout(function () { return setElement(null); }, 2000);
    }, []);
    var handleHighlightError = (0, react_1.useCallback)(function (highlightedError) {
        setElement({ value: highlightedError, type: ScrollType.Error });
    }, []);
    var handleHighlightAttribute = (0, react_1.useCallback)(function (highlightedAttribute) {
        setTimeout(function () { return setElement({ value: highlightedAttribute, type: ScrollType.NewAttribute }); }, 300);
    }, []);
    var handleHighlightView = (0, react_1.useCallback)(function (value) {
        setElement({ value: value, type: ScrollType.NewView });
    }, []);
    var contextValue = (0, react_1.useMemo)(function () { return ({
        element: element === null || element === void 0 ? void 0 : element.value,
        type: element === null || element === void 0 ? void 0 : element.type,
        highlightView: handleHighlightView,
        highlightError: handleHighlightError,
        highlightAttribute: handleHighlightAttribute,
        scrollIntoRef: scrollIntoRef
    }); }, [element, scrollIntoRef]);
    (0, react_1.useEffect)(function () {
        return function () { return clearTimeout(highlightTimer.current); };
    }, []);
    return react_1.default.createElement(exports.ScrollToElementContext.Provider, { value: contextValue }, children);
};
exports.ScrollToElementProvider = ScrollToElementProvider;
