// (C) 2021-2026 GoodData Corporation
import { merge } from "lodash-es";
import { put, select } from "redux-saga/effects";
import { findSection, serializeLayoutSectionPath } from "../../../_staging/layout/coordinates.js";
import { invalidArgumentsProvided } from "../../events/general.js";
import { layoutSectionHeaderChanged, } from "../../events/layout.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectLayout } from "../../store/tabs/layout/layoutSelectors.js";
import { sanitizeHeader } from "./utils.js";
import { validateSectionExists } from "./validation/layoutValidation.js";
export function* changeLayoutSectionHeaderHandler(ctx, cmd) {
    const layout = yield select(selectLayout);
    const { index, header, merge: mergeHeaders } = cmd.payload;
    const isLegacyCommand = typeof index === "number";
    if (isLegacyCommand) {
        if (!validateSectionExists(layout, index)) {
            throw invalidArgumentsProvided(ctx, cmd, `Attempting to modify header of non-existent section at ${index}. There are currently ${layout.sections.length} sections.`);
        }
    }
    else {
        if (!validateSectionExists(layout, index)) {
            throw invalidArgumentsProvided(ctx, cmd, `Attempting to modify header of non-existent section at ${serializeLayoutSectionPath(index)}.`);
        }
    }
    const existingHeader = isLegacyCommand
        ? (layout.sections[index].header ?? {})
        : (findSection(layout, index).header ?? {});
    const newHeader = mergeHeaders ? merge({}, existingHeader, header) : header;
    const sanitizedHeader = sanitizeHeader(newHeader);
    const sectionPath = isLegacyCommand ? { parent: undefined, sectionIndex: index } : index;
    yield put(tabsActions.changeSectionHeader({
        index: sectionPath,
        header: sanitizedHeader,
        undo: {
            cmd,
        },
    }));
    return layoutSectionHeaderChanged(ctx, sanitizedHeader, sectionPath.sectionIndex, sectionPath, cmd.correlationId);
}
//# sourceMappingURL=changeLayoutSectionHeaderHandler.js.map