{"version":3,"file":"Gen.mjs","names":[],"sources":["../esm/Gen.js"],"sourcesContent":["const fileName_1 = \"(@fncts/schema) src/Gen.ts\";\nimport * as tsplus_module_1 from \"@fncts/schema/AST\";\nimport * as tsplus_module_2 from \"@fncts/schema/ASTAnnotation\";\nimport * as tsplus_module_3 from \"@fncts/test/control/Gen/api\";\nimport * as tsplus_module_4 from \"@fncts/io/IO/api\";\nimport * as tsplus_module_5 from \"@fncts/base/collection/immutable/Vector/api\";\nimport * as tsplus_module_6 from \"@fncts/base/data/Maybe/destructors\";\nimport * as tsplus_module_7 from \"@fncts/base/data/Maybe/api\";\nimport * as tsplus_module_8 from \"@fncts/base/data/Maybe/definition\";\nimport * as tsplus_module_9 from \"@fncts/base/data/Either/api\";\nimport { InvalidInterpretationError } from \"@fncts/schema/InvalidInterpretationError\";\nimport { memoize } from \"@fncts/schema/utils\";\n/**\n * @tsplus getter fncts.schema.Schema genFrom\n */\nexport function genFrom(self) {\n    return go(tsplus_module_1.getFrom(self.ast));\n}\n/**\n * @tsplus getter fncts.schema.Schema genTo\n */\nexport function genTo(self) {\n    return go(self.ast);\n}\nfunction getHook(ast) {\n    return ast.annotations.get(tsplus_module_2.GenHook);\n}\nfunction record(key, value) {\n    return tsplus_module_3.map((tuples) => {\n        const out = {};\n        for (const [k, v] of tuples) {\n            out[k] = v;\n        }\n        return out;\n    })(tsplus_module_3.arrayWith({ maxLength: 10 })(tsplus_module_3.tuple(key, value)));\n}\nconst go = memoize(function go(ast) {\n    void 0;\n    switch (ast._tag) {\n        case 0 /* ASTTag.Declaration */:\n            return tsplus_module_6.match(() => tsplus_module_3.fromIO(tsplus_module_4.haltNow(new InvalidInterpretationError(\"cannot build a Gen for a Declaration without a Gen hook\"), fileName_1 + \":49:23\")), (hook) => hook(...tsplus_module_5.map(go)(ast.typeParameters)))(getHook(ast));\n        case 1 /* ASTTag.Literal */:\n            return tsplus_module_3.constant(ast.literal);\n        case 2 /* ASTTag.UniqueSymbol */:\n            return tsplus_module_3.constant(ast.symbol);\n        case 3 /* ASTTag.UndefinedKeyword */:\n            return tsplus_module_3.constant(undefined);\n        case 4 /* ASTTag.VoidKeyword */:\n            return tsplus_module_3.constant(undefined);\n        case 5 /* ASTTag.NeverKeyword */:\n            return tsplus_module_3.fromIO(tsplus_module_4.haltNow(new InvalidInterpretationError(\"cannot build a Gen for `never`\"), fileName_1 + \":62:35\"));\n        case 6 /* ASTTag.UnknownKeyword */:\n            return tsplus_module_3.anything();\n        case 7 /* ASTTag.AnyKeyword */:\n            return tsplus_module_3.anything();\n        case 8 /* ASTTag.StringKeyword */:\n            return tsplus_module_3.fullUnicodeString();\n        case 9 /* ASTTag.NumberKeyword */:\n            return tsplus_module_3.float;\n        case 10 /* ASTTag.BooleanKeyword */:\n            return tsplus_module_3.boolean;\n        case 11 /* ASTTag.BigIntKeyword */:\n            return tsplus_module_3.bigInt;\n        case 12 /* ASTTag.SymbolKeyword */:\n            return tsplus_module_3.map((s) => Symbol.for(s))(tsplus_module_3.fullUnicodeString());\n        case 13 /* ASTTag.ObjectKeyword */:\n            return tsplus_module_3.anything();\n        case 15 /* ASTTag.TemplateLiteral */: {\n            const components = [tsplus_module_3.constant(ast.head)];\n            for (const span of ast.spans) {\n                components.push(tsplus_module_3.fullUnicodeString({ maxLength: 5 }));\n                components.push(tsplus_module_3.constant(span.literal));\n            }\n            return tsplus_module_3.map((spans) => spans.join(\"\"))(tsplus_module_3.tuple(...components));\n        }\n        case 16 /* ASTTag.Tuple */: {\n            const elements = tsplus_module_5.map((e) => go(e.type))(ast.elements);\n            const rest = tsplus_module_7.map((restElement) => tsplus_module_5.map(go)(restElement))(ast.rest);\n            let output = tsplus_module_3.tuple(...elements);\n            if (elements.length > 0 && tsplus_module_8.isNothing(rest)) {\n                const firstOptionalIndex = tsplus_module_5.findIndex((e) => e.isOptional)(ast.elements);\n                if (firstOptionalIndex !== -1) {\n                    output = tsplus_module_3.flatMap((as) => tsplus_module_3.map((i) => as.slice(0, i))(tsplus_module_3.intWith({ min: firstOptionalIndex, max: elements.length - 1 })))(output);\n                }\n            }\n            if (tsplus_module_8.isJust(rest)) {\n                const head = tsplus_module_5.unsafeHead(rest.value);\n                const tail = tsplus_module_5.tail(rest.value);\n                output = tsplus_module_3.flatMap((as) => tsplus_module_3.map((rest) => [...as, ...rest])(tsplus_module_3.arrayWith({ maxLength: 5 })(head)))(output);\n                for (let j = 0; j < tail.length; j++) {\n                    output = tsplus_module_3.flatMap((as) => tsplus_module_3.map((a) => [...as, a])(tsplus_module_5.unsafeGet(j)(tail)))(output);\n                }\n            }\n            return output;\n        }\n        case 17 /* ASTTag.TypeLiteral */: {\n            const propertySignatureTypes = tsplus_module_5.map((ps) => go(ps.type))(ast.propertySignatures);\n            const indexSignatures = tsplus_module_5.map((is) => [go(is.parameter), go(is.type)])(ast.indexSignatures);\n            const requiredGens = {};\n            const optionalGens = {};\n            for (let i = 0; i < propertySignatureTypes.length; i++) {\n                const ps = tsplus_module_5.unsafeGet(i)(ast.propertySignatures);\n                const name = ps.name;\n                if (!ps.isOptional) {\n                    requiredGens[name] = tsplus_module_5.unsafeGet(i)(propertySignatureTypes);\n                }\n                else {\n                    optionalGens[name] = tsplus_module_5.unsafeGet(i)(propertySignatureTypes);\n                }\n            }\n            let output = tsplus_module_3.zipWith(tsplus_module_3.partial(optionalGens), (a, b) => ({ ...a, ...b }))(tsplus_module_3.struct(requiredGens));\n            for (let i = 0; i < indexSignatures.length; i++) {\n                const parameter = tsplus_module_5.unsafeGet(i)(indexSignatures)[0];\n                const type = tsplus_module_5.unsafeGet(i)(indexSignatures)[1];\n                output = tsplus_module_3.flatMap((o) => {\n                    return tsplus_module_3.map((d) => ({ ...d, ...o }))(record(parameter, type));\n                })(output);\n            }\n            return output;\n        }\n        case 18 /* ASTTag.Union */: {\n            const types = tsplus_module_5.map(go)(ast.types);\n            return tsplus_module_3.oneOf(...types);\n        }\n        case 19 /* ASTTag.Lazy */: {\n            return tsplus_module_6.match(() => {\n                const f = () => go(ast.getAST());\n                const get = memoize(f);\n                return tsplus_module_3.defer(() => get(f));\n            }, (handler) => handler())(getHook(ast));\n        }\n        case 14 /* ASTTag.Enum */: {\n            if (ast.enums.length === 0) {\n                return tsplus_module_3.fromIO(tsplus_module_4.haltNow(new InvalidInterpretationError(\"cannot build a Gen for an empty enum\"), fileName_1 + \":149:37\"));\n            }\n            return tsplus_module_3.oneOf(...tsplus_module_5.map(([_, value]) => tsplus_module_3.constant(value))(ast.enums));\n        }\n        case 20 /* ASTTag.Refinement */: {\n            const from = go(ast.from);\n            return tsplus_module_6.match(() => tsplus_module_3.filter((a) => tsplus_module_9.isRight(ast.decode(a)))(from), (handler) => handler(from))(getHook(ast));\n        }\n        case 21 /* ASTTag.Transform */:\n            return go(ast.to);\n        case 22 /* ASTTag.Validation */: {\n            const from = go(ast.from);\n            return tsplus_module_6.match(() => tsplus_module_3.filter((a) => tsplus_module_5.every((v) => v.validate(a))(ast.validation))(from), (handler) => handler(from))(getHook(ast));\n        }\n    }\n});\n//# sourceMappingURL=Gen.js.map"],"mappings":";;;;;;;;;;;;AAAA,MAAM,aAAa;;;;AAenB,SAAgB,QAAQ,MAAM;AAC1B,QAAO,GAAG,gBAAgB,QAAQ,KAAK,IAAI,CAAC;;;;;AAKhD,SAAgB,MAAM,MAAM;AACxB,QAAO,GAAG,KAAK,IAAI;;AAEvB,SAAS,QAAQ,KAAK;AAClB,QAAO,IAAI,YAAY,IAAI,gBAAgB,QAAQ;;AAEvD,SAAS,OAAO,KAAK,OAAO;AACxB,QAAO,gBAAgB,KAAK,WAAW;EACnC,MAAM,MAAM,EAAE;AACd,OAAK,MAAM,CAAC,GAAG,MAAM,OACjB,KAAI,KAAK;AAEb,SAAO;GACT,CAAC,gBAAgB,UAAU,EAAE,WAAW,IAAI,CAAC,CAAC,gBAAgB,MAAM,KAAK,MAAM,CAAC,CAAC;;AAEvF,MAAM,KAAK,QAAQ,SAAS,GAAG,KAAK;AAEhC,SAAQ,IAAI,MAAZ;EACI,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