import React, { useCallback, useEffect, useRef, useState } from 'react';
import { isNil } from 'ramda';
import { createContext } from '@fluentui/react-context-selector';
export var ChangeRequestEditorContext = createContext({
    updateChange: undefined,
    getLineData: undefined,
    getLinesByChangeId: undefined,
    rejectChange: undefined,
    revertChangeUpdateOrReject: undefined,
    getIsParentLineRejected: undefined,
    getIsLineRejected: undefined,
    getIsLineUpdated: undefined,
    getAllChanges: undefined,
    setErrors: undefined,
    getErrorByLineId: undefined,
    getErrors: undefined
});
var emptyArray = [];
export var ChangeRequestEditorProvider = function (_a) {
    var children = _a.children;
    var _b = useState(new Map()), lineDataMap = _b[0], setLineDataMap = _b[1];
    var lineDataMapRef = useRef(lineDataMap);
    var _c = useState([]), errors = _c[0], setErrors = _c[1];
    var updateChange = useCallback(function (lineIds, data) {
        setLineDataMap(function (prevMap) {
            var newMap = new Map(prevMap);
            lineIds.forEach(function (id) {
                newMap.set(id, { value: data });
            });
            return newMap;
        });
    }, []);
    var rejectChange = useCallback(function (lineIds) {
        setLineDataMap(function (prevMap) {
            var newMap = new Map(prevMap);
            lineIds.forEach(function (id) {
                newMap.set(id, { isRejected: true });
            });
            return newMap;
        });
    }, []);
    var revertChangeUpdateOrReject = useCallback(function (lineIds) {
        setLineDataMap(function (prevMap) {
            var newMap = new Map(prevMap);
            lineIds.forEach(function (id) {
                newMap.forEach(function (_, key) {
                    if (key.startsWith("".concat(id, "/")) || key === id) {
                        newMap.delete(key);
                    }
                });
            });
            return newMap;
        });
    }, []);
    var getLineData = useCallback(function (lineId) {
        return lineDataMap.get(lineId);
    }, [lineDataMap]);
    var getIsLineRejected = useCallback(function (lineId) {
        var _a;
        return ((_a = lineDataMap.get(lineId)) === null || _a === void 0 ? void 0 : _a.isRejected) || false;
    }, [lineDataMap]);
    var getIsParentLineRejected = useCallback(function (lineId) {
        var lineIdParts = (lineId || '').split('/');
        var possibleRejectedIds = Array.from({ length: lineIdParts.length - 1 }).reduce(function (ids, _, index) {
            return ids.concat(lineIdParts.slice(0, index + 1).join('/'));
        }, []);
        return possibleRejectedIds.some(function (lineId) { var _a; return (_a = lineDataMap.get(lineId)) === null || _a === void 0 ? void 0 : _a.isRejected; });
    }, [lineDataMap]);
    var getIsLineUpdated = useCallback(function (lineId) {
        var _a;
        return !isNil((_a = lineDataMap.get(lineId)) === null || _a === void 0 ? void 0 : _a.value);
    }, [lineDataMap]);
    var getLinesByChangeId = useCallback(function (changeId) {
        return Array.from(lineDataMapRef.current).filter(function (_a) {
            var key = _a[0], _value = _a[1];
            return key.split('/')[1] === changeId;
        });
    }, []);
    var getAllChanges = useCallback(function () {
        var changes = Array.from(lineDataMapRef.current).map(function (_a) {
            var lineId = _a[0], data = _a[1];
            return ({ lineId: lineId, data: data });
        });
        return changes.length > 0 ? changes : emptyArray;
    }, []);
    useEffect(function () {
        lineDataMapRef.current = lineDataMap;
    }, [lineDataMap]);
    var getErrorByLineId = useCallback(function (lineId) {
        return errors.find(function (_a) {
            var lineIds = _a.lineIds;
            return lineIds.includes(lineId);
        });
    }, [errors]);
    var getErrors = useCallback(function () {
        return errors;
    }, [errors]);
    var contextValue = {
        updateChange: updateChange,
        getLineData: getLineData,
        getLinesByChangeId: getLinesByChangeId,
        rejectChange: rejectChange,
        revertChangeUpdateOrReject: revertChangeUpdateOrReject,
        getIsParentLineRejected: getIsParentLineRejected,
        getIsLineRejected: getIsLineRejected,
        getIsLineUpdated: getIsLineUpdated,
        getAllChanges: getAllChanges,
        setErrors: setErrors,
        getErrorByLineId: getErrorByLineId,
        getErrors: getErrors
    };
    return React.createElement(ChangeRequestEditorContext.Provider, { value: contextValue }, children);
};
