require "cgi"
require "json"

def render(call)
  raise "expected component_render" unless call.fetch("tool") == "component_render"

  args = call.fetch("args")
  raise "expected confirm component" unless args.fetch("kind") == "confirm"

  props = args.fetch("props")
  escape = ->(value) { CGI.escapeHTML(value.to_s) }

  [
    %(<section data-component="confirm" data-variant="#{escape.call(props.fetch("variant"))}" data-id="#{escape.call(args.fetch("id"))}">),
    %(  <h3>#{escape.call(props.fetch("title"))}</h3>),
    %(  <p>#{escape.call(props.fetch("body"))}</p>),
    %(  <footer>),
    %(    <button data-action="cancel">#{escape.call(props.fetch("cancel_label"))}</button>),
    %(    <button data-action="confirm">#{escape.call(props.fetch("confirm_label"))}</button>),
    %(  </footer>),
    %(</section>)
  ].join("\n")
end

path = ARGV.fetch(0, "tool-call.json")
puts render(JSON.parse(File.read(path)))
