module SvelteOnRails::ViewHelpers

def cached_svelte_component(path, props = {}, html: {}, options: {})

def cached_svelte_component(path, props = {}, html: {}, options: {})
  support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, true)
  support.render_cached(self) do
    render_component(support)
  end
end

def cached_svelte_component_ruby2(path, *args)

def cached_svelte_component_ruby2(path, *args)
  props, html, options = validate_ruby2_props(args)
  support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, true)
  support.render_cached(self) do
    render_component(support)
  end
end

def render_component(support)

def render_component(support)
  if support.ssr?
    ssr_result = support.render_ssr
    content_tag(:div, support.html_options) do
      concat(content_tag(:style, ssr_result['css'], type: 'text/css'))
      concat(ssr_result['html'].html_safe)
    end
  else
    content_tag(:div, support.html_options) {}
  end
end

def svelte_component(path, props = {}, html: {}, options: {})

def svelte_component(path, props = {}, html: {}, options: {})
  support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, false)
  support.render(self) do
    render_component(support)
  end
end

def svelte_component_ruby2(path, *args)

def svelte_component_ruby2(path, *args)
  props, html, options = validate_ruby2_props(args)
  support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, false)
  support.render(self) do
    render_component(support)
  end
end

def validate_ruby2_props(args)

def validate_ruby2_props(args)
  last_is_kwargs = false
  kw_err = false
  if args.length > 2
    kw_err = true
  elsif args.length >= 1
    if args.last.keys & [:html, :options]
      last_is_kwargs = true
      if (args.last.keys - [:html, :options]).present?
        kw_err = true
      end
    end
  end
  if kw_err
    raise "Invalid arguments: First argument can be a hash as svelte-properties, second and third arguments are keyword arguments: :html and/or :options"
  end
  first_is_props = args.length == 1 && !last_is_kwargs || args.length == 2
  [
    (first_is_props ? args.first : nil),
    (last_is_kwargs ? args[1][:html] : nil),
    (last_is_kwargs ? args[1][:options] : nil),
  ]
end