{"version":3,"file":"Tag-CToQb9Fc.js","sources":["../../../app/frontend/shared/Tag.ts"],"sourcesContent":["type ShortTag = [string, string | ShortTag[]] | [];\ntype TagArrayable = string | Tag | Tag[] | ShortTag[] | null;\n\nexport default class Tag {\n #tag: string;\n #length: number;\n #value: string | Tag[];\n\n static getStringValue(tag: string, input: TagArrayable): string | undefined {\n const foundTag = this.toTagArray(input).find((t) => t.tag === tag);\n if (typeof foundTag?.value === \"string\") {\n return foundTag.value;\n }\n\n return undefined;\n }\n\n static from(tag: string, input: string | TagArrayable): Tag {\n let length: number;\n let value: string | Tag[];\n\n if (typeof input === \"string\") {\n value = input;\n length = Array.from(input).length;\n } else {\n value = this.toTagArray(input);\n length = value.reduce((acc, tag) => acc + tag.length + 4, 0);\n }\n\n return new this(tag, length, value);\n }\n\n static toString(input: TagArrayable): string {\n return this.toTagArray(input)\n .map((tag) => tag.toString())\n .join(\"\");\n }\n\n static toTagArray(input?: TagArrayable): Tag[] {\n let tagList: Tag[] = [];\n\n if (typeof input === \"string\") {\n tagList = this.#parse(input);\n } else if (input instanceof Tag) {\n tagList.push(input);\n } else if (input != null) {\n if (input.every((value) => Array.isArray(value))) {\n input.forEach(([k, v]) => {\n if (k && v) {\n tagList.push(this.from(k, v));\n }\n });\n } else {\n tagList = input;\n }\n }\n\n return tagList;\n }\n\n static create(input: TagArrayable): string {\n const inputWithChecksumTag = [\n ...this.toTagArray(input),\n new this(\"63\", 4, \"\"),\n ];\n let output = this.toString(inputWithChecksumTag);\n output += this.#calculateChecksum(output);\n return output;\n }\n\n static debug(input: TagArrayable): void {\n this.traverse(input, (tag, path) => {\n const indent = \"\\t\".repeat(path.length + 1);\n if (typeof tag.value === \"string\") {\n console.log(\n `${indent}${tag.tag} ${tag.length.toString(10)} ${tag.value}`,\n );\n } else {\n console.log(`${indent}${tag.tag} ${tag.length.toString(10)}`);\n }\n });\n }\n\n static traverse(\n input: TagArrayable,\n func: (tag: Tag, path: Tag[]) => void,\n path: Tag[] = [],\n ) {\n this.toTagArray(input).forEach((tag) => {\n func(tag, path);\n if (typeof tag.value !== \"string\") {\n this.traverse(tag.value, func, [...path, tag]);\n }\n });\n }\n\n static #parse(input?: string, path: string[] = []): Tag[] {\n const tags: Tag[] = [];\n\n if (input == null) {\n return tags;\n }\n\n const unicodeCodepoints = Array.from(input);\n\n let index = 0;\n let currentTag: string;\n let currentLength: number;\n let currentValue: string | Tag[];\n\n while (index < unicodeCodepoints.length) {\n currentTag = unicodeCodepoints.slice(index, index + 2).join(\"\");\n currentLength = parseInt(\n unicodeCodepoints.slice(index + 2, index + 4).join(\"\"),\n 10,\n );\n currentValue = unicodeCodepoints\n .slice(index + 4, index + currentLength + 4)\n .join(\"\");\n\n const newPath = [...path, currentTag];\n if (this.#isTemplate(newPath)) {\n currentValue = this.#parse(currentValue, newPath);\n }\n\n tags.push(new this(currentTag, currentLength, currentValue));\n\n index += currentLength + 4;\n }\n\n return tags;\n }\n\n static #isTemplate(path: string[]): boolean {\n const pathTags = path.map((p) => parseInt(p, 10));\n if (path.length === 1) {\n if (pathTags[0] >= 26 && pathTags[0] <= 51) {\n // Merchant Account Information\n return true;\n }\n\n if (pathTags[0] >= 80 && pathTags[0] <= 99) {\n // Unreserved Templates\n return true;\n }\n\n if (pathTags[0] === 62) {\n // Additional Data Field Template\n return true;\n }\n\n if (pathTags[0] === 64) {\n // Merchant Information Language Template\n return true;\n }\n } else if (path.length === 2) {\n if (pathTags[0] === 62 && pathTags[1] >= 50 && pathTags[1] <= 99) {\n // Payment System specific templates\n return true;\n }\n }\n\n return false;\n }\n\n static #calculateChecksum(input: string): string {\n let crc = 0xffff;\n const polynomial = 0x1021;\n const textEncoder = new TextEncoder();\n const bytes = textEncoder.encode(input); // The checksum is computed over the UTF-8 encoded string\n\n for (let i = 0; i < bytes.length; i++) {\n const byte = bytes[i];\n for (let j = 0; j < 8; j++) {\n const bit = ((byte >> (7 - j)) & 1) === 1;\n const c15 = ((crc >> 15) & 1) === 1;\n crc <<= 1;\n if (c15 !== bit) {\n crc ^= polynomial;\n }\n }\n }\n\n crc &= 0xffff;\n return crc.toString(16).toUpperCase().padStart(4, \"0\");\n }\n\n private constructor(tag: string, length: number, value: string | Tag[]) {\n this.#tag = tag;\n this.#length = length;\n this.#value = value;\n\n if (tag.length !== 2) {\n throw new Error(\"tag must be 2 digits\");\n }\n\n if (length < 0 || length > 99) {\n throw new Error(\"length can only be 0 to 99\");\n }\n }\n\n get tag() {\n return this.#tag;\n }\n\n get length() {\n return this.#length;\n }\n\n get value() {\n return this.#value;\n }\n\n toString() {\n let value: string;\n\n if (typeof this.#value === \"string\") {\n value = this.#value;\n } else {\n value = this.#value.map((tag) => tag.toString()).join(\"\");\n }\n\n return `${this.#tag}${this.#length.toString(10).padStart(2, \"0\")}${value}`;\n }\n}\n"],"names":["_tag","_length","_value","_Tag_static","parse_fn","isTemplate_fn","calculateChecksum_fn","_Tag","tag","length","value","__privateAdd","__privateSet","input","foundTag","t","acc","tagList","__privateMethod","k","v","inputWithChecksumTag","output","path","indent","func","__privateGet","tags","unicodeCodepoints","index","currentTag","currentLength","currentValue","newPath","pathTags","p","crc","polynomial","bytes","i","byte","j","bit","c15","Tag"],"mappings":"yXAGA,IAAAA,EAAAC,EAAAC,EAAAC,EAAAC,EAAAC,EAAAC,EAAA,MAAqBC,EAArB,MAAqBA,CAAI,CAwLf,YAAYC,EAAaC,EAAgBC,EAAuB,CAvLxEC,EAAA,KAAAX,GACAW,EAAA,KAAAV,GACAU,EAAA,KAAAT,GA0LM,GAJJU,EAAA,KAAKZ,EAAOQ,GACZI,EAAA,KAAKX,EAAUQ,GACfG,EAAA,KAAKV,EAASQ,GAEVF,EAAI,SAAW,EACX,MAAA,IAAI,MAAM,sBAAsB,EAGpC,GAAAC,EAAS,GAAKA,EAAS,GACnB,MAAA,IAAI,MAAM,4BAA4B,CAC9C,CA9LF,OAAO,eAAeD,EAAaK,EAAyC,CACpE,MAAAC,EAAW,KAAK,WAAWD,CAAK,EAAE,KAA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