#include "cell.h"
#include "wildmatch.h"

JSC_CCALL(wildstar_match,
  const char *pattern = JS_ToCString(js, argv[0]);
  const char *string = JS_ToCString(js, argv[1]);
  int flags = 0;
  if (argc > 2)
    flags = js2number(js, argv[2]);
    
  int result = wildmatch(pattern, string, flags);
  
  JS_FreeCString(js, pattern);
  JS_FreeCString(js, string);
  
  return JS_NewBool(js, result == WM_MATCH);
)

static const JSCFunctionListEntry js_wildstar_funcs[] = {
  MIST_FUNC_DEF(wildstar, match, 3),
  JS_PROP_INT32_DEF("WM_MATCH", WM_MATCH, 0),
  JS_PROP_INT32_DEF("WM_NOMATCH", WM_NOMATCH, 0),
  JS_PROP_INT32_DEF("WM_NOESCAPE", WM_NOESCAPE, 0),
  JS_PROP_INT32_DEF("WM_PATHNAME", WM_PATHNAME, 0),
  JS_PROP_INT32_DEF("WM_PERIOD", WM_PERIOD, 0),
  JS_PROP_INT32_DEF("WM_LEADING_DIR", WM_LEADING_DIR, 0),
  JS_PROP_INT32_DEF("WM_CASEFOLD", WM_CASEFOLD, 0),
  JS_PROP_INT32_DEF("WM_WILDSTAR", WM_WILDSTAR, 0),
};

JSValue js_core_internal_wildstar_use(JSContext *js) {
  JS_FRAME(js);
  JS_ROOT(mod, JS_NewObject(js));
  JS_SetPropertyFunctionList(js, mod.val, js_wildstar_funcs, countof(js_wildstar_funcs));
  JS_RETURN(mod.val);
}
