<commit_before>package slate
package app

import cats.Applicative
import cats.data.Validated
import cats.implicits._
import monix.eval.Task
import org.atnos.eff._
import org.atnos.eff.syntax.all._
import org.scalajs.dom.XMLHttpRequest
import qq.Json
import qq.Platform.Rec._
import qq.cc.{CompiledFilter, CompiledFilterStack, OrCompilationError, OrRuntimeErr, Prelude, QQRuntimeError, QQRuntimeException, RuntimeErrs, TypeError}
import qq.data.{CompiledDefinition, JSON}
import qq.util.Recursion.RecursionEngine
import qq.util._
import slate.ajax.{Ajax, AjaxMethod}

import scala.concurrent.duration._
import scala.scalajs.js

object SlatePrelude extends Prelude {

  import CompiledDefinition.noParamDefinition
  import QQRuntimeException._

  def googleAuth: CompiledDefinition =
    noParamDefinition("googleAuth",
      CompiledFilter.constE(identify.getAuthToken(interactive = true).map[List[JSON]](JSON.Str(_) :: Nil).parallel.send[CompiledFilterStack]))

  def launchAuth: CompiledDefinition =
    CompiledDefinition("launchAuth", 2, CompiledDefinition.standardEffectDistribution {
      case List(urlRaw, queryParamsRaw) => _ =>
        val urlVerified: Validated[RuntimeErrs, String] = urlRaw match {
          case JSON.Str(s) => s.validNel
          case k => (TypeError("ajax", "object" -> k): QQRuntimeError).invalidNel
        }
        val queryParamsVerified: Validated[RuntimeErrs, JSON.ObjList] = queryParamsRaw match {
          case o: JSON.ObjMap => JSON.ObjList(o.value.toList).validNel
          case o: JSON.ObjList => o.validNel
          case k => (TypeError("ajax", "object" -> k): QQRuntimeError).invalidNel
        }
        val urlWithQueryParams = Applicative[Validated[RuntimeErrs, ?]].map2(urlVerified, queryParamsVerified)(Ajax.addQueryParams)
        for {
          webAuthResult <-
          urlWithQueryParams.toEither.send[CompiledFilterStack].flatMap(identify.launchWebAuthFlow(interactive = true, _).parallel.send[CompiledFilterStack])
          accessToken = webAuthResult.substring(webAuthResult.indexOf("&code=") + "&code=".length)
        } yield JSON.obj("code" -> JSON.Str(accessToken)) :: Nil
    })

  private def makeAjaxDefinition(name: String, ajaxMethod: AjaxMethod) = CompiledDefinition(name, 4,
    CompiledDefinition.standardEffectDistribution {
      case List(urlRaw, queryParamsRaw, dataRaw, headersRaw) => _ =>
        type Stack = Fx.fx2[TaskParallel, OrRuntimeErr]
        implicit val ajaxTimeout = Ajax.Timeout(2000.millis)
        val urlValidated: Validated[RuntimeErrs, String] = urlRaw match {
          case JSON.Str(s) => s.validNel
          case k => (TypeError("ajax", "string" -> k): QQRuntimeError).invalidNel
        }
        val queryParamsValidated: Validated[RuntimeErrs, JSON.ObjList] = queryParamsRaw match {
          case o: JSON.ObjMap => JSON.ObjList(o.value.toList).validNel
          case o: JSON.ObjList => o.validNel
          case k => (TypeError("ajax", "object" -> k): QQRuntimeError).invalidNel
        }
        val dataValidated: Validated[RuntimeErrs, String] = dataRaw match {
          case JSON.Str(s) => s.validNel
          case o: JSON.Obj => JSON.render(o).validNel
          case k => (TypeError("ajax", "string | object" -> k): QQRuntimeError).invalidNel
        }
        val headersValidated: Validated[RuntimeErrs, Map[String, String]] = headersRaw match {
          case o: JSON.ObjList if o.value.forall(_._2.isInstanceOf[JSON.Str]) => o.toMap.value.mapValues(_.asInstanceOf[JSON.Str].value).validNel
          case o: JSON.ObjMap if o.value.forall(_._2.isInstanceOf[JSON.Str]) => o.toMap.value.mapValues(_.asInstanceOf[JSON.Str].value).validNel
          case k => (TypeError("ajax", "object" -> k): QQRuntimeError).invalidNel
        }
        Eff.collapse[Stack, TaskParallel, List[JSON]](for {
          resp <-
          Eff.collapse[Stack, OrRuntimeErr, XMLHttpRequest](
            (urlValidated |@| dataValidated |@| queryParamsValidated |@| headersValidated).map(
              Ajax(ajaxMethod, _, _, _, _, withCredentials = false, "")
                .onErrorRestart(1)
                .map(Either.right)
                .onErrorHandle[OrRuntimeErr[XMLHttpRequest]] {
                case e: QQRuntimeException => Either.left[RuntimeErrs, XMLHttpRequest](e.errors)
              }.parallel<commit_msg>Removed partial List(...) patterns in favor of head and tail<commit_after>package slate
package app

import cats.Applicative
import cats.data.Validated
import cats.implicits._
import monix.eval.Task
import org.atnos.eff._
import org.atnos.eff.syntax.all._
import org.scalajs.dom.XMLHttpRequest
import qq.Json
import qq.Platform.Rec._
import qq.cc.{CompiledFilter, CompiledFilterStack, OrCompilationError, OrRuntimeErr, Prelude, QQRuntimeError, QQRuntimeException, RuntimeErrs, TypeError}
import qq.data.{CompiledDefinition, JSON}
import qq.util.Recursion.RecursionEngine
import qq.util._
import slate.ajax.{Ajax, AjaxMethod}

import scala.concurrent.duration._
import scala.scalajs.js

object SlatePrelude extends Prelude {

  import CompiledDefinition.noParamDefinition
  import QQRuntimeException._

  def googleAuth: CompiledDefinition =
    noParamDefinition("googleAuth",
      CompiledFilter.constE(identify.getAuthToken(interactive = true).map[List[JSON]](JSON.Str(_) :: Nil).parallel.send[CompiledFilterStack]))

  def launchAuth: CompiledDefinition =
    CompiledDefinition("launchAuth", 2, CompiledDefinition.standardEffectDistribution {
      params => _ =>
        val urlRaw = params.head
        val queryParamsRaw = params.tail.head
        val urlVerified: Validated[RuntimeErrs, String] = urlRaw match {
          case JSON.Str(s) => s.validNel
          case k => (TypeError("ajax", "object" -> k): QQRuntimeError).invalidNel
        }
        val queryParamsVerified: Validated[RuntimeErrs, JSON.ObjList] = queryParamsRaw match {
          case o: JSON.ObjMap => JSON.ObjList(o.value.toList).validNel
          case o: JSON.ObjList => o.validNel
          case k => (TypeError("ajax", "object" -> k): QQRuntimeError).invalidNel
        }
        val urlWithQueryParams = Applicative[Validated[RuntimeErrs, ?]].map2(urlVerified, queryParamsVerified)(Ajax.addQueryParams)
        for {
          webAuthResult <-
          Eff.send[OrRuntimeErr, CompiledFilterStack, String](urlWithQueryParams.toEither)
            .flatMap(identify.launchWebAuthFlow(interactive = true, _).parallel.send[CompiledFilterStack])
          accessToken = webAuthResult.substring(webAuthResult.indexOf("&code=") + "&code=".length)
        } yield JSON.obj("code" -> JSON.Str(accessToken)) :: Nil
    })

  private def makeAjaxDefinition(name: String, ajaxMethod: AjaxMethod) = CompiledDefinition(name, 4,
    CompiledDefinition.standardEffectDistribution {
      params =>  _ =>
        val urlRaw = params.head
        val queryParamsRaw = params.tail.head
        val dataRaw = params.tail.tail.head
        val headersRaw = params.tail.tail.tail.head
        type Stack = Fx.fx2[TaskParallel, OrRuntimeErr]
        implicit val ajaxTimeout = Ajax.Timeout(2000.millis)
        val urlValidated: Validated[RuntimeErrs, String] = urlRaw match {
          case JSON.Str(s) => s.validNel
          case k => (TypeError("ajax", "string" -> k): QQRuntimeError).invalidNel
        }
        val queryParamsValidated: Validated[RuntimeErrs, JSON.ObjList] = queryParamsRaw match {
          case o: JSON.ObjMap => JSON.ObjList(o.value.toList).validNel
          case o: JSON.ObjList => o.validNel
          case k => (TypeError("ajax", "object" -> k): QQRuntimeError).invalidNel
        }
        val dataValidated: Validated[RuntimeErrs, String] = dataRaw match {
          case JSON.Str(s) => s.validNel
          case o: JSON.Obj => JSON.render(o).validNel
          case k => (TypeError("ajax", "string | object" -> k): QQRuntimeError).invalidNel
        }
        val headersValidated: Validated[RuntimeErrs, Map[String, String]] = headersRaw match {
          case o: JSON.ObjList if o.value.forall(_._2.isInstanceOf[JSON.Str]) => o.toMap.value.mapValues(_.asInstanceOf[JSON.Str].value).validNel
          case o: JSON.ObjMap if o.value.forall(_._2.isInstanceOf[JSON.Str]) => o.toMap.value.mapValues(_.asInstanceOf[JSON.Str].value).validNel
          case k => (TypeError("ajax", "object" -> k): QQRuntimeError).invalidNel
        }
        Eff.collapse[Stack, TaskParallel, List[JSON]](for {
          resp <-
          Eff.collapse[Stack, OrRuntimeErr, XMLHttpRequest](
            (urlValidated |@| dataValidated |@| queryParamsValidated |@| headersValidated).map(
              Ajax(ajaxMethod, _, _, _, _, withCredentials = false, "")
                .onErrorRestart(1)
                .map(Either.right)
                .onErrorHandle[OrRuntimeErr[XMLHttpRequest]] {
                case e: QQRuntimeException => Either.left[RuntimeErrs, XMLHttpRequest](e.errors)
              }.parallel
{'fraction_non_alphanumeric': 0.11648524295814162, 'fraction_numerical': 0.0024688587139490518, 'mean_word_length': 3.3304178814382897, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3392752', 'n_tokens_mistral': 2727, 'n_tokens_neox': 2671, 'n_words': 517}