"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaveLayoutButton = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_redux_1 = require("react-redux");
const AdaptableContext_1 = require("../AdaptableContext");
const ButtonSave_1 = require("../Components/Buttons/ButtonSave");
const GridRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/GridRedux"));
const LayoutRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/LayoutRedux"));
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const SaveLayoutButton = () => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const accessLevel = adaptable.api.entitlementApi.getEntitlementAccessLevelForModule(ModuleConstants.LayoutModuleId);
    const dispatch = (0, react_redux_1.useDispatch)();
    const currentDraftLayout = (0, react_redux_1.useSelector)((state) => state.Grid.CurrentLayout);
    const isManualSaveLayout = !adaptable.api.layoutApi.shouldAutoSaveLayouts();
    const handleSave = React.useCallback((layout) => {
        dispatch(LayoutRedux.LayoutSave(layout));
        // Cannot be se to null, because it needs to be up to date when it is updated
        dispatch(GridRedux.LayoutUpdateCurrentDraft(layout));
    }, []);
    const isSaveEnabled = currentDraftLayout && !adaptable.api.layoutApi.internalApi.areDraftAndCurrentLayoutEqual();
    if (!isManualSaveLayout) {
        return React.createElement(React.Fragment, null);
    }
    return (React.createElement(ButtonSave_1.ButtonSave, { ml: 1, variant: "text", iconSize: 15, onClick: () => handleSave(currentDraftLayout), tooltip: "Save Changes to Current Layout", disabled: !isSaveEnabled, accessLevel: accessLevel }));
};
exports.SaveLayoutButton = SaveLayoutButton;
