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

const Node = @import("../../Node.zig");
const Element = @import("../../Element.zig");
const HtmlElement = @import("../Html.zig");

const Dialog = @This();

_proto: *HtmlElement,

pub fn asElement(self: *Dialog) *Element {
    return self._proto._proto;
}
pub fn asConstElement(self: *const Dialog) *const Element {
    return self._proto._proto;
}
pub fn asNode(self: *Dialog) *Node {
    return self.asElement().asNode();
}

pub fn getOpen(self: *const Dialog) bool {
    return self.asConstElement().getAttributeSafe(comptime .wrap("open")) != null;
}

pub fn setOpen(self: *Dialog, open: bool, frame: *Frame) !void {
    if (open) {
        try self.asElement().setAttributeSafe(comptime .wrap("open"), .wrap(""), frame);
    } else {
        try self.asElement().removeAttribute(comptime .wrap("open"), frame);
    }
}

pub fn getReturnValue(self: *const Dialog) []const u8 {
    return self.asConstElement().getAttributeSafe(comptime .wrap("returnvalue")) orelse "";
}

pub fn setReturnValue(self: *Dialog, value: []const u8, frame: *Frame) !void {
    try self.asElement().setAttributeSafe(comptime .wrap("returnvalue"), .wrap(value), frame);
}

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

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

    pub const open = bridge.accessor(Dialog.getOpen, Dialog.setOpen, .{});
    pub const returnValue = bridge.accessor(Dialog.getReturnValue, Dialog.setReturnValue, .{});
};

const testing = @import("../../../../testing.zig");
test "WebApi: HTML.Dialog" {
    try testing.htmlRunner("element/html/dialog.html", .{});
}
