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7C,CAAC;SAAM,IAAG,IAAI,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC;QAC5B,WAAW,GAAG,wBAAwB,CAAC;IAC3C,CAAC;IAED,OAAO,QAAQ,IAAI,CAAC,IAAI,MAAM,WAAW,EAAE,CAAC;AAChD,CAAC","sourcesContent":["import { Class, Property, File, getCommentHeader, getInheritanceTree, Context } from \"../types\";\nimport { GenerateOptions } from \"../api\";\n\nconst typeMaps: { [key: string]: string } = {\n    \"string\": \"string\",\n    \"number\": \"number\",\n    \"boolean\": \"boolean\",\n    \"int8\": \"number\",\n    \"uint8\": \"number\",\n    \"int16\": \"number\",\n    \"uint16\": \"number\",\n    \"int32\": \"number\",\n    \"uint32\": \"number\",\n    \"int64\": \"number\",\n    \"uint64\": \"number\",\n    \"float32\": \"number\",\n    \"float64\": \"number\",\n}\n\nconst distinct = (value: string, index: number, self: string[]) =>\n    self.indexOf(value) === index;\n\nexport function generate (context: Context, options: GenerateOptions): File[] {\n    return context.classes.map(klass => ({\n        name: klass.name + \".js\",\n        content: generateClass(klass, options.namespace, context.classes)\n    }));\n}\n\nfunction generateClass(klass: Class, namespace: string, allClasses: Class[]) {\n    const allRefs: Property[] = [];\n    klass.properties.forEach(property => {\n        let type = property.type;\n\n        // keep all refs list\n        if ((type === \"ref\" || type === \"array\" || type === \"map\")) {\n            allRefs.push(property);\n        }\n    });\n\n    return `${getCommentHeader()}\n\nconst schema = require(\"@colyseus/schema\");\nconst Schema = schema.Schema;\nconst type = schema.type;\n${allRefs.\n    filter(ref => ref.childType && typeMaps[ref.childType] === undefined).\n    map(ref => ref.childType).\n    concat(getInheritanceTree(klass, allClasses, false).map(klass => klass.name)).\n    filter(distinct).\n    map(childType => `const ${childType} = require(\"./${childType}\");`).\n    join(\"\\n\")}\n\nclass ${klass.name} extends ${klass.extends} {\n    constructor () {\n        super();\n${klass.properties.\n    filter(prop => prop.childType !== undefined).\n    map(prop => \"        \" + generatePropertyInitializer(prop)).join(\"\\n\")}\n    }\n}\n${klass.properties.map(prop => generatePropertyDeclaration(klass.name, prop)).join(\"\\n\")}\n\nexport default ${klass.name};\n`;\n}\n\nfunction generatePropertyDeclaration(className: string, prop: Property) {\n    let typeArgs: string;\n\n    if (prop.childType) {\n        const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n\n        if (isUpcaseFirst) {\n            typeArgs += `, ${prop.childType}`;\n\n        } else {\n            typeArgs += `, \"${prop.childType}\"`;\n        }\n\n        if(prop.type === \"ref\") {\n            typeArgs = `${prop.childType}`;\n\n        } else if(prop.type === \"array\") {\n            typeArgs = (isUpcaseFirst)\n                ? `[ ${prop.childType} ]`\n                : `[ \"${prop.childType}\" ]`;\n\n        } else if(prop.type === \"map\") {\n            typeArgs = (isUpcaseFirst)\n                ? `{ map: ${prop.childType} }`\n                : `{ map: \"${prop.childType}\" }`;\n        }\n\n    } else {\n        typeArgs = `\"${prop.type}\"`;\n    }\n\n    return `type(${typeArgs})(${className}.prototype, \"${prop.name}\");`;\n}\n\nfunction generatePropertyInitializer(prop: Property) {\n    let initializer = \"\";\n\n    if(prop.type === \"ref\") {\n        initializer = `new ${prop.childType}()`;\n\n    } else if(prop.type === \"array\") {\n        initializer = `new schema.ArraySchema()`;\n\n    } else if(prop.type === \"map\") {\n        initializer = `new schema.MapSchema()`;\n    }\n\n    return `this.${prop.name} = ${initializer}`;\n}\n"]}