// Copyright (C) 2023-2025  Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");

const js = @import("../../js/js.zig");
const Frame = @import("../../Frame.zig");

const Event = @import("../Event.zig");
const UIEvent = @import("UIEvent.zig");
const Window = @import("../Window.zig");

const String = lp.String;

const CompositionEvent = @This();

_proto: *UIEvent,
_data: []const u8 = "",

const CompositionEventOptions = struct {
    data: ?[]const u8 = null,
};

const Options = Event.inheritOptions(CompositionEvent, CompositionEventOptions);

pub fn init(typ: []const u8, opts_: ?Options, frame: *Frame) !*CompositionEvent {
    const arena = try frame.getArena(.tiny, "CompositionEvent");
    errdefer frame.releaseArena(arena);
    const type_string = try String.init(arena, typ, .{});

    const opts = opts_ orelse Options{};
    const event = try frame._factory.uiEvent(
        arena,
        type_string,
        CompositionEvent{
            ._proto = undefined,
            ._data = if (opts.data) |str| try arena.dupe(u8, str) else "",
        },
    );

    Event.populatePrototypes(event, opts, false);
    return event;
}

pub fn asEvent(self: *CompositionEvent) *Event {
    return self._proto.asEvent();
}

pub fn getData(self: *const CompositionEvent) []const u8 {
    return self._data;
}

pub fn initCompositionEvent(
    self: *CompositionEvent,
    typ: []const u8,
    bubbles: ?bool,
    cancelable: ?bool,
    view: ?*Window,
    data: ?[]const u8,
) !void {
    const ui = self._proto;
    const event = ui._proto;
    if (event._event_phase != .none) {
        return;
    }

    const arena = event._arena;
    event._type_string = try String.init(arena, typ, .{});
    event._bubbles = bubbles orelse false;
    event._cancelable = cancelable orelse false;
    ui._view = view;
    self._data = if (data) |d| try arena.dupe(u8, d) else "";
}

pub const JsApi = struct {
    pub const bridge = js.Bridge(CompositionEvent);

    pub const Meta = struct {
        pub const name = "CompositionEvent";
        pub const prototype_chain = bridge.prototypeChain();
        pub var class_id: bridge.ClassId = undefined;
    };

    pub const constructor = bridge.constructor(CompositionEvent.init, .{});
    pub const data = bridge.accessor(CompositionEvent.getData, null, .{});
    pub const initCompositionEvent = bridge.function(CompositionEvent.initCompositionEvent, .{});
};

const testing = @import("../../../testing.zig");
test "WebApi: CompositionEvent" {
    try testing.htmlRunner("event/composition.html", .{});
}
