import { Requirement } from "@siteimprove/alfa-act" ; import { Branched } from "@siteimprove/alfa-branched" ; import { None , Option } from "@siteimprove/alfa-option" ; import { Criteria } from "./criterion/data" ; public export class Criterion < C extends Criterion . Chapter = Criterion . Chapter > extends Requirement { public static of < C extends Criterion . Chapter > ( chapter ) { return new Criterion ( chapter ) ; } private readonly _chapter : C ; private constructor ( chapter ) { super ( ) ; this . _chapter = chapter ; } /**
     * The chapter of this criterion.
     */ public get chapter ( ) : C { return this . _chapter ; } /**
     * The title of this criterion.
     */ public get title ( ) : Criterion . Title < C > { return Criteria [ this . _chapter ] . title ; } remarks public get uri ( ) : Criterion . URI < C , "2.1" | "2.2" > { const [ , { uri } ] = [ ... Criteria [ this . _chapter ] . versions ] . find ( ( [ version ] ) => version === Criterion . Version . Recommendation || version === Criterion . Version . Draft ) ! ; return uri as Criterion . URI < C , "2.1" | "2.2" > ; } /**
     * The versions in which this criterion is defined.
     */ public get versions ( ) : Iterable < Criterion . Version > { return [ ... Criteria [ this . _chapter ] . versions ] . map ( ( [ version ] ) => version ) ; } remarks public get level ( ) : Branched < Criterion . Level , Criterion . Version > { return Branched . from ( [ ... Criteria [ this . _chapter ] . versions ] . map ( ( [ version , { level } ] ) => [ level , [ version ] , ] ) ) ; } public toJSON ( ) { const { title } = Criteria [ this . _chapter ] ; return { ... super . toJSON ( ) , chapter : this . _chapter , title , } ; } public toEARL ( ) { const { title } = Criteria [ this . _chapter ] ; return { ... super . toEARL ( ) , "@context" : { earl : "http://www.w3.org/ns/earl#" , dct : "http://purl.org/dc/terms/" , } , "dct:title" : title , "dct:isPartOf" : "https://www.w3.org/TR/WCAG/" , } ; } } public export namespace Criterion { export interface JSON extends Requirement . JSON { chapter : Chapter ; title : string ; } export interface EARL extends Requirement . EARL { "@context" : { earl : "http://www.w3.org/ns/earl#" ; dct : "http://purl.org/dc/terms/" ; } ; "dct:title" : string ; "dct:isPartOf" : "https://www.w3.org/TR/WCAG/" ; } /**
     * The chapters of all criteria.
     */ export type Chapter = keyof Criteria ; export function isChapter ( value ) { return value in Criteria ; } /**
     * The title of the specified criterion.
     */ export type Title < C extends Chapter = Chapter > = Criteria [ C ] [ "title" ] ; /**
     * The different versions of the WCAG.
     */ export type Version = "2.0" | "2.1" | "2.2" ; export namespace Version { /**
         * The current version of the WCAG Recommendation.
         */ export const Recommendation = "2.1" ; /**
         * The current version of the WCAG Recommendation.
         */ export type Recommendation = typeof Recommendation ; /**
         * The current version of the WCAG Working Draft.
         */ export const Draft = "2.2" ; /**
         * The current version of the WCAG Working Draft.
         */ export type Draft = typeof Draft ; } /**
     * The URI of the specified criterion.
     */ export type URI < C extends Chapter = Chapter , V extends Version = Version > = Criteria [ C ] [ "versions" ] extends Iterable < infer T > ? T extends readonly [ V , { readonly uri : infer U ; } ] ? U : never : never ; /**
     * The level of the specified criterion under the specified version.
     */ export type Level < C extends Chapter = Chapter , V extends Version = Version > = Criteria [ C ] [ "versions" ] extends Iterable < infer T > ? T extends readonly [ V , { readonly level : infer L ; } ] ? L : never : never ; export namespace Level { /**
         * All criteria of the specified level under the specific version.
         */ type Of < L extends Level , V extends Version = Version . Recommendation > = { [ C in Chapter ] : L extends Level < C , V > ? C : never ; } [ Chapter ] ; remarks export type A < V extends Version = Version . Recommendation > = Of < "A" , V > ; remarks export type AA < V extends Version = Version . Recommendation > = Of < "AA" , V > ; remarks export type AAA < V extends Version = Version . Recommendation > = Of < "AAA" , V > ; } export function isCriterion ( value ) { return value instanceof Criterion ; } export function fromURI ( uri ) { for ( const [ chapter , value ] of Object . entries ( Criteria ) ) { for ( const version of value . versions ) { if ( version [ 1 ] . uri === uri ) { return Option . of ( Criterion . of ( chapter as Chapter ) ) ; } } } return None ; } }