text: "use strict";
  /* eslint-disable no-console */
  Object.defineProperty(exports, "__esModule", { value: true });
  const assert = require("assert");
  const fs = require("fs");
  const consts_1 = require("../consts");
  const demo_1 = require("../demo");
  const props_1 = require("../props");
  function parseDemoFile(path) {
      fs.readFile(path, (err, buffer) => {
          assert.ifError(err);
          const demoFile = new demo_1.DemoFile();
          demoFile.entities.on("datatablesready", () => {
              console.log("// DO NOT MODIFY!");
              console.log("// Auto-generated by ./generators/sendtabletypes.ts");
              console.log("");
              console.log('import { EntityHandle } from "./entityhandle";');
              console.log();
              console.log("export interface Vector {\n  readonly x: number;\n  readonly y: number;\n  readonly z: number;\n}");
              console.log();
              for (const dt of demoFile.entities.dataTables) {
                  console.log(`export interface ${dt.netTableName} {`);
                  let lastElemType;
                  for (const prop of dt.props) {
                      let typeStr;
                      if ((prop.flags & props_1.SPROP_EXCLUDE) !== 0) {
                          console.log(`  // ${prop.dtName}.${prop.varName} - excluded`);
                          continue;
                      }
                      if (prop.type === 0 /* Int */) {
                          if (prop.numBits === 1) {
                              typeStr = "boolean";
                          }
                          else if (prop.numBits === consts_1.NUM_NETWORKED_EHANDLE_BITS &&
                              (prop.flags & props_1.SPROP_UNSIGNED) !== 0 &&
                              (prop.flags & props_1.SPROP_NOSCALE) !== 0) {
                              typeStr = "EntityHandle";
                          }
                          else {
                              typeStr = "number";
                          }
                      }
                      else if (prop.type === 1 /* Float */) {
                          typeStr = "number";
                      }
                      else if (prop.type === 2 /* Vector */) {
                          typeStr = "Vector";
                      }
                      else if (prop.type === 3 /* VectorXY */) {
                          typeStr = "Vector";
                      }
                      else if (prop.type === 4 /* String */) {
                          typeStr = "string";
                      }
                      else if (prop.type === 5 /* Array */) {
                          if (typeof lastElemType === "undefined")
                              throw new Error("Array prop type was not preceded by SPROP_INSIDEARRAY");
                          typeStr = `ReadonlyArray<${lastElemType}>`;
                      }
                      else if (prop.type === 6 /* DataTable */) {
                          console.log(`  // ${prop.varName}: DataTable;`);
                          continue;
                      }
                      else if (prop.type === 7 /* Int64 */) {
                          typeStr = "Long";
                      }
                      else {
                          throw new Error(`Unexpected prop type ${prop.type}`);
                      }
                      // Skip inside array - we'll print the PropType.Array
                      // which will follow immediately after this prop
                      if ((prop.flags & props_1.SPROP_INSIDEARRAY) !== 0) {
                          console.log(`  // ${prop.varName}: ${typeStr} - InsideArray;`);
                          lastElemType = typeStr;
                          continue;
                      }
                      let name = prop.varName;
                      if (name.indexOf(`"`) !== -1) {
                          // Wrap in [`foo`]
                          name = "[`" + name + "`]";
                      }
                      else if (name.charCodeAt(0) < 65 ||
                          name.indexOf(".") !== -1 ||
                          name.indexOf("[") !== -1) {
                          // Wrap in ["foo"]
                          name = `["${name}"]`;
                      }
                      console.log(`  ${name}: ${typeStr};`);
                  }
                  console.log("}");
                  console.log();
              }
              for (const serverClass of demoFile.entities.serverClasses) {
                  const dataTableNames = serverClass.flattenedProps.reduce((names, fp) => {
                      if (names.indexOf(fp.table.netTableName) === -1) {
                          names.push(fp.table.netTableName);
                      }
                      return names;
                  }, []);
                  console.log(`export interface ${serverClass.name} {`);
                  for (const dataTable of dataTableNames) {
                      if (dataTable === "DT_AnimTimeMustBeFirst") {
                          continue;
                      }
                      console.log(`  ${dataTable}: ${dataTable};`);
                  }
                  console.log(`}`);
                  console.log("");
              }
              demoFile.cancel();
          });
          demoFile.parse(buffer);
      });
  }
  if (process.argv.length == 3) {
      parseDemoFile(process.argv[2]);
  }
  else {
      console.error("expected args: <path to .dem>");
      process.exitCode = 1;
  }
  //# sourceMappingURL=sendtabletypes.js.map