/*
 * Copyright (c) 2023-2025, Sam Atkins <sam@ladybird.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/CSS/Flex.h>
#include <LibWeb/CSS/Percentage.h>
#include <LibWeb/CSS/Serialize.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/CSS/StyleValues/FlexStyleValue.h>

namespace Web::CSS {

Flex::Flex(double value, FlexUnit unit)
    : m_unit(unit)
    , m_value(value)
{
}

Flex Flex::make_fr(double value)
{
    return { value, FlexUnit::Fr };
}

Flex Flex::from_style_value(NonnullRefPtr<StyleValue const> const& value)
{
    if (value->is_flex())
        return value->as_flex().flex();

    if (value->is_calculated())
        return value->as_calculated().resolve_flex({}).value();

    VERIFY_NOT_REACHED();
}

Flex Flex::percentage_of(Percentage const& percentage) const
{
    return Flex { percentage.as_fraction() * m_value, m_unit };
}

void Flex::serialize(StringBuilder& builder, SerializationMode serialization_mode) const
{
    // https://drafts.csswg.org/cssom/#serialize-a-css-value
    // AD-HOC: No spec definition, so copy the other <dimension> definitions
    if (serialization_mode == SerializationMode::ResolvedValue) {
        serialize_a_number(builder, to_fr());
        builder.append("fr"sv);
        return;
    }
    serialize_a_number(builder, raw_value());
    builder.append(unit_name());
}

String Flex::to_string(SerializationMode serialization_mode) const
{
    StringBuilder builder;
    serialize(builder, serialization_mode);
    return builder.to_string_without_validation();
}

double Flex::to_fr() const
{
    return ratio_between_units(m_unit, FlexUnit::Fr) * m_value;
}

}
