from fasthtml.components import *

from myfasthtml.core.bindings import Binding
from myfasthtml.core.commands import Command
from myfasthtml.core.utils import merge_classes


class mk:
  
  @staticmethod
  def button(element, command: Command = None, binding: Binding = None, **kwargs):
    return mk.mk(Button(element, **kwargs), command=command, binding=binding)
  
  @staticmethod
  def icon(icon, size=20,
           can_select=True,
           can_hover=False,
           cls='',
           command: Command = None,
           binding: Binding = None,
           **kwargs):
    merged_cls = merge_classes(f"mf-icon-{size}",
                               'icon-btn' if can_select else '',
                               'mmt-btn' if can_hover else '',
                               cls,
                               kwargs)
    
    return mk.mk(Div(icon, cls=merged_cls, **kwargs), command=command, binding=binding)
  
  @staticmethod
  def manage_command(ft, command: Command):
    if command:
      htmx = command.get_htmx_params()
      ft.attrs |= htmx
    
    return ft
  
  @staticmethod
  def manage_binding(ft, binding: Binding, ft_attr=None, init_binding=True):
    if not binding:
      return ft
    
    binding.bind_ft(ft, ft_attr)
    if init_binding:
      binding.init()
      # as it is the first binding, remove the hx-swap-oob
      if "hx-swap-oob" in ft.attrs:
        del ft.attrs["hx-swap-oob"]
    
    return ft
  
  @staticmethod
  def mk(ft, command: Command = None, binding: Binding = None, init_binding=True):
    ft = mk.manage_command(ft, command)
    ft = mk.manage_binding(ft, binding, init_binding=init_binding)
    return ft
