"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.ChangeRequestEditorProvider = exports.ChangeRequestEditorContext = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var react_context_selector_1 = require("@fluentui/react-context-selector");
exports.ChangeRequestEditorContext = (0, react_context_selector_1.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 = [];
var ChangeRequestEditorProvider = function (_a) {
    var children = _a.children;
    var _b = (0, react_1.useState)(new Map()), lineDataMap = _b[0], setLineDataMap = _b[1];
    var lineDataMapRef = (0, react_1.useRef)(lineDataMap);
    var _c = (0, react_1.useState)([]), errors = _c[0], setErrors = _c[1];
    var updateChange = (0, react_1.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 = (0, react_1.useCallback)(function (lineIds) {
        setLineDataMap(function (prevMap) {
            var newMap = new Map(prevMap);
            lineIds.forEach(function (id) {
                newMap.set(id, { isRejected: true });
            });
            return newMap;
        });
    }, []);
    var revertChangeUpdateOrReject = (0, react_1.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 = (0, react_1.useCallback)(function (lineId) {
        return lineDataMap.get(lineId);
    }, [lineDataMap]);
    var getIsLineRejected = (0, react_1.useCallback)(function (lineId) {
        var _a;
        return ((_a = lineDataMap.get(lineId)) === null || _a === void 0 ? void 0 : _a.isRejected) || false;
    }, [lineDataMap]);
    var getIsParentLineRejected = (0, react_1.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 = (0, react_1.useCallback)(function (lineId) {
        var _a;
        return !(0, ramda_1.isNil)((_a = lineDataMap.get(lineId)) === null || _a === void 0 ? void 0 : _a.value);
    }, [lineDataMap]);
    var getLinesByChangeId = (0, react_1.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 = (0, react_1.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;
    }, []);
    (0, react_1.useEffect)(function () {
        lineDataMapRef.current = lineDataMap;
    }, [lineDataMap]);
    var getErrorByLineId = (0, react_1.useCallback)(function (lineId) {
        return errors.find(function (_a) {
            var lineIds = _a.lineIds;
            return lineIds.includes(lineId);
        });
    }, [errors]);
    var getErrors = (0, react_1.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_1.default.createElement(exports.ChangeRequestEditorContext.Provider, { value: contextValue }, children);
};
exports.ChangeRequestEditorProvider = ChangeRequestEditorProvider;
