<commit_before>jsoniq version "1.0";

module namespace request = "http://apps.28.io/request";

import module namespace req = "http://www.28msec.com/modules/http-request";
import module namespace entities = "http://28.io/modules/xbrl/entities";
import module namespace archives = "http://28.io/modules/xbrl/archives";
import module namespace multiplexer = "http://28.io/modules/xbrl/profiles/multiplexer";
import module namespace hypercubes = "http://28.io/modules/xbrl/hypercubes";
import module namespace api = "http://apps.28.io/api";

declare function request:request(
  $profile-name as string,
  $entities as object*,
  $request-uri as string?,
  $format as string?,
  $concept as string*,
  $fiscalYear as string*,
  $fiscalPeriod as string*,
  $fiscalPeriodType as string*,
  $cik as string*,
  $tag as string*,
  $ticker as string*,
  $sic as string*,
  $aid as string*,
  $open as boolean) as object
{
  let $request as object :=
    {
      profile-name: $profile-name,
      format: api:preprocess-format($format, $request-uri),
      concept: [ $concept ],
      fiscalYear: [ $fiscalYear ],
      fiscalPeriod: [ api:preprocess-fiscal-periods($fiscalPeriod) ],
      fiscalPeriodType: [ $fiscalPeriodType ],
      cik: [ $cik ],
      tag: [ api:preprocess-tags($tag) ],
      ticker: [ $ticker ],
      sic: [ $sic ],
      aid: [ $aid ],
      open: $open
    }
  let $dimensions as object* :=
    for $parameter in request:param-names($request)
    group by $dimension-name :=
      switch(true)
      case ends-with(lower-case($parameter), "::default")
        return substring-before($parameter, "::default")
      case ends-with(lower-case($parameter), ":default")
        return substring-before($parameter, ":default")
      case ends-with(lower-case($parameter), "::type")
        return substring-before($parameter, "::type")
      default
        return $parameter
    let $all as boolean :=
      (request:param-values($dimension-name, $request, $entities) ! upper-case(string($$))) = "ALL"
    let $type as string? :=
      (request:param-values($dimension-name || "::type", $request, $entities),
       request:param-values($dimension-name || ":type", $request, $entities))[1]

    let $values := request:param-values($dimension-name, $request, $entities)
    let $typed-values := if (exists($type)) then request:cast-sequence($values[$$ ne "ALL"], $type) else $values

    let $has-default := ($parameter = $dimension-name || "::default") or ($parameter = $dimension-name || ":default")
    let $default-value := (request:param-values($dimension-name || "::default", $request, $entities),
                           request:param-values($dimension-name || ":default", $request, $entities))[1]
    let $typed-default-value := if (exists($type)) then request:cast-sequence($default-value, $type) else $default-value

    return
    {
       $dimension-name : {|
         { "Type" : $type }[exists($type)],
         { "Domain" : [ $typed-values ] }
             [exists($typed-values) and not($all)],
         { "Default" : $typed-default-value }[$has-default],
         { "Default" : null }
             [$all and (not $has-default) and exists($type)],
         { "Default" : "xbrl28:Domain" }
             [$all and (not $has-default) and empty($type)]
       |}
    }
  return
    {|
      $request,
      { dimensions: {| $dimensions |} }
    |}
};

declare function request:param-values(
  $name as string) as string*
{
  req:param-values($name)
};

declare %private function request:param-values(
    $name as string,
    $request as object,
    $entities as object*) as string*
{
  let $profile-name as string := $request.profile-name
  let $concept as string* := $request.concept[]
  let $fiscalYear as string* := $request.fiscalYear[]
  let $fiscalPeriod as string* := $request.fiscalPeriod[]
  let $fiscalPeriodType as string* := $request.fiscalPeriodType[]
  let $cik as string* := $request.cik[]
  let $tag as string* := $request.tag[]
  let $ticker as string* := $request.ticker[]
  let $sic as string* := $request.sic[]
  let $aid as string* := $request.aid[]
  return
    switch(true)
     case $name eq "xbrl:Concept"
        return ($concept, req:param-values("xbrl:Concept"))

    (: sec profile :)
     case $name eq "sec:FiscalYear" and $profile-name eq "sec"
        return ($fiscalYear, req:param-values("sec:FiscalYear"))[$$ ne "LATEST"]
     case $name eq "sec:FiscalYear::type" and $profile-name eq "sec"
        return "integer"
     case $name eq "sec:FiscalPeriod" and $profile-name eq "sec"
        return ($fiscalPeriod, req:param-values("sec:FiscalPeriod"))
     case $name eq "sec:FiscalPeriod::type" and $profile-name eq "sec"
        return "string"
     case $name eq "sec:FiscalPeriodType" and $profile-name eq "sec"
        return ($fiscalPeriodType, req:param-values("sec:FiscalPeriodType"))
     case $name eq "sec:FiscalPeriodType::type" and $profile-name eq "sec"
        return "string"
     case $name eq "dei:LegalEntityAxis" and $profile-name eq "sec"
        return
         if(empty((req:param-values("sec:LegalEntityAxis"), req:param-values("sec:LegalEntityAxis::default"))))
         then "sec:DefaultLegalEntity"
         else req:param-values("sec:LegalEntityAxis")
     case $name eq "dei:LegalEntityAxis::default" return
         if(empty((req:param-values("sec:LegalEntityAxis"), req:param-values("sec:LegalEntityAxis::default"))))
         then "sec:DefaultLegalEntity"
         else req:param-values("sec:LegalEntityAxis::default")

     case $name eq "xbrl:Entity" and $profile-name = ("sec", "japan")
         return (
                if(empty(($cik,$tag,$ticker,$sic)) or exists($entities))
                then entities:eid($entities)
                else "dummy",
                req:param-values("xbrl:Entity")
            )
     case $name eq "xbrl28:Archive" and $profile-name = ("sec", "japan") return (
            let $prefix as string :=
                switch($profile-name)
                case "sec" return "sec"
                case "japan" return "fsa"
                default return (: not reachable :) ()
            let $fiscalYears :=
                let $years := req:param-values( $prefix || ":FiscalYear")
                return if ($years) then $years else $fiscalYear
            let $fiscalPeriods := request:param-values($prefix || ":FiscalPeriod", $request, $entities)
            return
                if($fiscalYears = "LATEST")
                then archives:aid(multiplexer:latest-filings($profile-name, $entities, $fiscalPeriods))
                else (),
            $aid,
            req:param-values("xbrl28:Archive")
        )

     (: japan profile :)
     case $name eq "fsa:FiscalYear" and $profile-name eq "japan"
     return ($fiscalYear ! string($$), req:param-values("fsa:FiscalYear"))[$$ ne "LATEST"]
     case $name eq "fsa:FiscalYear::type" and $profile-name eq "japan"
     return "integer"
     case $name eq "fsa:FiscalPeriod" and $profile-name eq "japan"
     return ($fiscalPeriod, req:param-values("fsa:FiscalPeriod"))
     case $name eq "fsa:FiscalPeriod::type" and $profile-name eq "japan"
     return "string"
     case $name eq "fsa:FiscalPeriodType" and $profile-name eq "japan"
     return ($fiscalPeriodType, req:param-values("fsa:FiscalPeriodType"))
     case $name eq "fsa:FiscalPeriodType::type" and $profile-name eq "japan"
     return "string"
     case $name eq "fsa:ArchiveFiscalYear::type" and $profile-name eq "japan"
     return "integer"
     case $name eq "fsa:ArchiveFiscalPeriod::type" and $profile-name eq "japan"
     return "string"

     default return req:param-values($name)
};

declare %private function request:param-names($request as object) as string*<commit_msg>:bug: Fix error upon entering several fiscal years.<commit_after>jsoniq version "1.0";

module namespace request = "http://apps.28.io/request";

import module namespace req = "http://www.28msec.com/modules/http-request";
import module namespace entities = "http://28.io/modules/xbrl/entities";
import module namespace archives = "http://28.io/modules/xbrl/archives";
import module namespace multiplexer = "http://28.io/modules/xbrl/profiles/multiplexer";
import module namespace hypercubes = "http://28.io/modules/xbrl/hypercubes";
import module namespace api = "http://apps.28.io/api";

declare function request:request(
  $profile-name as string,
  $entities as object*,
  $request-uri as string?,
  $format as string?,
  $concept as string*,
  $fiscalYear as string*,
  $fiscalPeriod as string*,
  $fiscalPeriodType as string*,
  $cik as string*,
  $tag as string*,
  $ticker as string*,
  $sic as string*,
  $aid as string*,
  $open as boolean) as object
{
  let $request as object :=
    {
      profile-name: $profile-name,
      format: api:preprocess-format($format, $request-uri),
      concept: [ $concept ],
      fiscalYear: [ $fiscalYear ],
      fiscalPeriod: [ api:preprocess-fiscal-periods($fiscalPeriod) ],
      fiscalPeriodType: [ $fiscalPeriodType ],
      cik: [ $cik ],
      tag: [ api:preprocess-tags($tag) ],
      ticker: [ $ticker ],
      sic: [ $sic ],
      aid: [ $aid ],
      open: $open
    }
  let $dimensions as object* :=
    for $parameter in request:param-names($request)
    group by $dimension-name :=
      switch(true)
      case ends-with(lower-case($parameter), "::default")
        return substring-before($parameter, "::default")
      case ends-with(lower-case($parameter), ":default")
        return substring-before($parameter, ":default")
      case ends-with(lower-case($parameter), "::type")
        return substring-before($parameter, "::type")
      default
        return $parameter
    let $all as boolean :=
      (request:param-values($dimension-name, $request, $entities) ! upper-case(string($$))) = "ALL"
    let $type as string? :=
      (request:param-values($dimension-name || "::type", $request, $entities),
       request:param-values($dimension-name || ":type", $request, $entities))[1]

    let $values := request:param-values($dimension-name, $request, $entities)
    let $typed-values := if (exists($type)) then request:cast-sequence($values[$$ ne "ALL"], $type) else $values

    let $has-default := ($parameter = $dimension-name || "::default") or ($parameter = $dimension-name || ":default")
    let $default-value := (request:param-values($dimension-name || "::default", $request, $entities),
                           request:param-values($dimension-name || ":default", $request, $entities))[1]
    let $typed-default-value := if (exists($type)) then request:cast-sequence($default-value, $type) else $default-value

    return
    {
       $dimension-name : {|
         { "Type" : $type }[exists($type)],
         { "Domain" : [ $typed-values ] }
             [exists($typed-values) and not($all)],
         { "Default" : $typed-default-value }[$has-default],
         { "Default" : null }
             [$all and (not $has-default) and exists($type)],
         { "Default" : "xbrl28:Domain" }
             [$all and (not $has-default) and empty($type)]
       |}
    }
  return
    {|
      $request,
      { dimensions: {| $dimensions |} }
    |}
};

declare function request:param-values(
  $name as string) as string*
{
  req:param-values($name)
};

declare %private function request:param-values(
    $name as string,
    $request as object,
    $entities as object*) as string*
{
  let $profile-name as string := $request.profile-name
  let $concept as string* := $request.concept[]
  let $fiscalYear as string* := $request.fiscalYear[]
  let $fiscalPeriod as string* := $request.fiscalPeriod[]
  let $fiscalPeriodType as string* := $request.fiscalPeriodType[]
  let $cik as string* := $request.cik[]
  let $tag as string* := $request.tag[]
  let $ticker as string* := $request.ticker[]
  let $sic as string* := $request.sic[]
  let $aid as string* := $request.aid[]
  return
    switch(true)
     case $name eq "xbrl:Concept"
        return ($concept, req:param-values("xbrl:Concept"))

    (: sec profile :)
     case $name eq "sec:FiscalYear" and $profile-name eq "sec"
        return ($fiscalYear, req:param-values("sec:FiscalYear"))[$$ ne "LATEST"]
     case $name eq "sec:FiscalYear::type" and $profile-name eq "sec"
        return "integer"
     case $name eq "sec:FiscalPeriod" and $profile-name eq "sec"
        return ($fiscalPeriod, req:param-values("sec:FiscalPeriod"))
     case $name eq "sec:FiscalPeriod::type" and $profile-name eq "sec"
        return "string"
     case $name eq "sec:FiscalPeriodType" and $profile-name eq "sec"
        return ($fiscalPeriodType, req:param-values("sec:FiscalPeriodType"))
     case $name eq "sec:FiscalPeriodType::type" and $profile-name eq "sec"
        return "string"
     case $name eq "dei:LegalEntityAxis" and $profile-name eq "sec"
        return
         if(empty((req:param-values("sec:LegalEntityAxis"), req:param-values("sec:LegalEntityAxis::default"))))
         then "sec:DefaultLegalEntity"
         else req:param-values("sec:LegalEntityAxis")
     case $name eq "dei:LegalEntityAxis::default" return
         if(empty((req:param-values("sec:LegalEntityAxis"), req:param-values("sec:LegalEntityAxis::default"))))
         then "sec:DefaultLegalEntity"
         else req:param-values("sec:LegalEntityAxis::default")

     case $name eq "xbrl:Entity" and $profile-name = ("sec", "japan")
         return (
                if(empty(($cik,$tag,$ticker,$sic)) or exists($entities))
                then entities:eid($entities)
                else "dummy",
                req:param-values("xbrl:Entity")
            )
     case $name eq "xbrl28:Archive" and $profile-name = ("sec", "japan") return (
            let $prefix as string :=
                switch($profile-name)
                case "sec" return "sec"
                case "japan" return "fsa"
                default return (: not reachable :) ()
            let $fiscalYears :=
                let $years := req:param-values( $prefix || ":FiscalYear")
                return if (exists($years)) then $years else $fiscalYear
            let $fiscalPeriods := request:param-values($prefix || ":FiscalPeriod", $request, $entities)
            return
                if($fiscalYears = "LATEST")
                then archives:aid(multiplexer:latest-filings($profile-name, $entities, $fiscalPeriods))
                else (),
            $aid,
            req:param-values("xbrl28:Archive")
        )

     (: japan profile :)
     case $name eq "fsa:FiscalYear" and $profile-name eq "japan"
     return ($fiscalYear ! string($$), req:param-values("fsa:FiscalYear"))[$$ ne "LATEST"]
     case $name eq "fsa:FiscalYear::type" and $profile-name eq "japan"
     return "integer"
     case $name eq "fsa:FiscalPeriod" and $profile-name eq "japan"
     return ($fiscalPeriod, req:param-values("fsa:FiscalPeriod"))
     case $name eq "fsa:FiscalPeriod::type" and $profile-name eq "japan"
     return "string"
     case $name eq "fsa:FiscalPeriodType" and $profile-name eq "japan"
     return ($fiscalPeriodType, req:param-values("fsa:FiscalPeriodType"))
     case $name eq "fsa:FiscalPeriodType::type" and $profile-name eq "japan"
     return "string"
     case $name eq "fsa:ArchiveFiscalYear::type" and $profile-name eq "japan"
     return "integer"
     case $name eq "fsa:ArchiveFiscalPeriod::type" and $profile-name eq "japan"
     return "string"

     default return req:param-values($name)
};

declare %private function request:param-names($request as object) as string*
{'fraction_non_alphanumeric': 0.15267027863777088, 'fraction_numerical': 0.0030959752321981426, 'mean_word_length': 3.1357695385436117, 'pattern_counts': {'":': 28, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '351074', 'n_tokens_mistral': 5003, 'n_tokens_neox': 4864, 'n_words': 1190}