/*
 * Copyright (c) 2026-present, the Ladybird developers
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/CSS/Invalidation/AdoptedStyleSheetInvalidator.h>
#include <LibWeb/CSS/StyleSheetInvalidation.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/StyleInvalidationReason.h>

namespace Web::CSS::Invalidation {

void invalidate_style_after_adopting_style_sheet(DOM::Node& document_or_shadow_root, CSSStyleSheet& style_sheet)
{
    style_sheet.add_owning_document_or_shadow_root(document_or_shadow_root);
    style_sheet.load_pending_image_resources(document_or_shadow_root.document());

    // Evaluate the sheet's media queries before the next style update so the cascade can see its rules. Otherwise the
    // rule cache may be rebuilt (e.g. via a :has() invalidation pass) before Document::evaluate_media_rules has a
    // chance to populate MediaList::m_matches.
    style_sheet.evaluate_media_queries(document_or_shadow_root.document());

    invalidate_style_for_stylesheet_change(document_or_shadow_root, style_sheet, DOM::StyleInvalidationReason::AdoptedStyleSheetsList);
}

void invalidate_style_after_removing_adopted_style_sheet(DOM::Node& document_or_shadow_root, CSSStyleSheet& style_sheet)
{
    style_sheet.remove_owning_document_or_shadow_root(document_or_shadow_root);
    invalidate_style_for_stylesheet_change(document_or_shadow_root, style_sheet, DOM::StyleInvalidationReason::AdoptedStyleSheetsList);
}

}
