/*
 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
 *           (C) 2000 Antti Koivisto (koivisto@kde.org)
 *           (C) 2000 Dirk Mueller (mueller@kde.org)
 * Copyright (C) 2003, 2005-2008, 2017 Apple Inc. All rights reserved.
 * Copyright (C) 2006 Graham Dennis (graham.dennis@gmail.com)
 * Copyright (C) 2025 Samuel Weinig <sam@webkit.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#pragma once

#include <WebCore/StylePrimitiveNumeric.h>
#include <WebCore/StyleTransformFunctionBase.h>

namespace WebCore {
namespace Style {

// translate() = translate( <length-percentage> , <length-percentage>? )
// https://drafts.csswg.org/css-transforms/#funcdef-transform-translate
// translate3d() = translate3d( <length-percentage> , <length-percentage> , <length> )
// https://drafts.csswg.org/css-transforms-2/#funcdef-translate3d
// translateX() = translateX( <length-percentage> )
// https://drafts.csswg.org/css-transforms/#funcdef-transform-translatex
// translateY() = translateY( <length-percentage> )
// https://drafts.csswg.org/css-transforms/#funcdef-transform-translatey
// translateZ() = translateZ( <length> )
// https://drafts.csswg.org/css-transforms-2/#funcdef-translatez

class TranslateTransformFunction final : public TransformFunctionBase {
public:
    using X = Style::LengthPercentage<CSS::AllUnzoomed>;
    using Y = Style::LengthPercentage<CSS::AllUnzoomed>;
    using Z = Style::Length<CSS::AllUnzoomed>;

    static Ref<const TranslateTransformFunction> create(const X&, const Y&, TransformFunctionBase::Type);
    static Ref<const TranslateTransformFunction> create(const X&, const Y&, const Z&, TransformFunctionBase::Type);

    Ref<const TransformFunctionBase> clone() const override;
    Ref<TransformOperation> toPlatform(const FloatSize&, ZoomFactor) const override;

    TransformFunctionBase::Type primitiveType() const override { return isRepresentableIn2D() ? Type::Translate : Type::Translate3D; }

    const X& x() const LIFETIME_BOUND { return m_x; }
    const Y& y() const LIFETIME_BOUND { return m_y; }
    Z z() const { return m_z; }

    bool isIdentity() const override { return m_x.isKnownZero() && m_y.isKnownZero() && m_z.isZero(); }
    bool isRepresentableIn2D() const override { return m_z.isZero(); }

    TransformFunctionSizeDependencies computeSizeDependencies() const override;

    bool operator==(const TranslateTransformFunction& other) const { return operator==(static_cast<const TransformFunctionBase&>(other)); }
    bool operator==(const TransformFunctionBase&) const override;

    void apply(TransformationMatrix&, const FloatSize& borderBoxSize, ZoomFactor) const override;

    Ref<const TransformFunctionBase> blend(const TransformFunctionBase* from, const BlendingContext&, bool blendToIdentity = false) const override;

    void dump(WTF::TextStream&) const override;

private:
    TranslateTransformFunction(const X&, const Y&, const Z&, TransformFunctionBase::Type);

    X m_x;
    Y m_y;
    Z m_z;
};

} // namespace Style
} // namespace WebCore

SPECIALIZE_TYPE_TRAITS_STYLE_TRANSFORM_FUNCTION(WebCore::Style::TranslateTransformFunction, WebCore::Style::TransformFunctionBase::isTranslateTransformFunctionType)
