"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tree_builder_seeds_exports = {};
__export(tree_builder_seeds_exports, {
  treeBuilderSeeder: () => treeBuilderSeeder
});
module.exports = __toCommonJS(tree_builder_seeds_exports);
var import_client = require("@prisma/pg/client");
var import_fs = __toESM(require("fs"));
const prisma = new import_client.PrismaClient();
function sanitizeNulls(obj) {
  if (obj === "null" || obj === "NULL") return null;
  if (Array.isArray(obj)) {
    return obj.map(sanitizeNulls);
  }
  if (obj && typeof obj === "object") {
    const newObj = {};
    for (const key in obj) {
      newObj[key] = sanitizeNulls(obj[key]);
    }
    return newObj;
  }
  return obj;
}
async function treeBuilderSeeder(company_id) {
  try {
    const fieldTypes = JSON.parse(
      import_fs.default.readFileSync(__dirname + "/json/field-types.json", "utf-8")
    );
    const treeBuilderEntities = JSON.parse(
      import_fs.default.readFileSync(__dirname + "/json/tree-builder.json", "utf-8")
    );
    const createOrUpdate = async (entity) => {
      const { lineitems, ...masterEntityData } = sanitizeNulls(entity);
      const mg = await prisma.masterEntity.upsert({
        where: {
          company_id_entity_name: {
            company_id,
            entity_name: masterEntityData.entity_name
          }
        },
        update: { ...masterEntityData },
        create: { ...masterEntityData, company_id }
      });
      const fieldTypeLength = fieldTypes?.length || 1;
      console.log("Processing entity:", fieldTypeLength, lineitems);
      if (lineitems && lineitems.length) {
        for (const lineitem of lineitems) {
          const field_type_id = lineitem.field_type_id + (company_id - 1) * fieldTypeLength;
          await prisma.masterEntityLineItem.upsert({
            where: {
              company_id_master_grid_id_field_name_display_name: {
                // this should match the name of your composite unique constraint
                company_id,
                master_grid_id: mg.id,
                field_name: lineitem.field_name,
                display_name: lineitem.display_name
              }
            },
            update: {
              ...lineitem,
              company_id,
              master_grid_id: mg.id,
              field_type_id
            },
            create: {
              ...lineitem,
              company_id,
              master_grid_id: mg.id,
              field_type_id
            }
          });
        }
      }
    };
    for (const entity of treeBuilderEntities) {
      await createOrUpdate(entity);
    }
    console.log("Tree builder entity creation completed.");
  } catch (error) {
    console.error("Seeder failed", error);
    throw error;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  treeBuilderSeeder
});
