"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 permission_seeds_exports = {};
__export(permission_seeds_exports, {
  permissionsSeeder: () => permissionsSeeder
});
module.exports = __toCommonJS(permission_seeds_exports);
var import_client = require("@prisma/pg/client");
var import_fs = __toESM(require("fs"));
const prisma = new import_client.PrismaClient();
const action_types = {
  view: "view",
  add: "add",
  edit: "edit",
  popup_add: "popup_add",
  popup_edit: "popup_edit",
  delete: "delete",
  details: "details",
  popup_details: "popup_details",
  export_excel: "export_excel",
  export_pdf: "export_pdf",
  import: "import",
  assign: "assign",
  email_resend: "email_resend",
  generate_vector: "generate_vector",
  child_details: "child_details",
  print: "print",
  reset_password: "reset_password",
  record_export: "record_export",
  get_code: "get_code"
};
function getCurrentUtcTime() {
  return (/* @__PURE__ */ new Date()).toISOString();
}
async function getMasterEntityIdBySlug(slug, company_id) {
  const masterEntity = await prisma.masterEntity.findFirst({
    where: {
      company_id,
      entity_name: slug
    }
  });
  return masterEntity ? masterEntity.id : null;
}
async function findOrCreatePermission(permissionData, company_id) {
  try {
    const utcNow = getCurrentUtcTime();
    let { rights, ...roleData } = permissionData;
    let order_no = 1;
    for (const right of rights) {
      const slug = action_types[right] + "_" + roleData.slug;
      const masterEntityId = await getMasterEntityIdBySlug(
        roleData.slug,
        company_id
      );
      await prisma.permissions.upsert({
        where: {
          company_id_slug: {
            // this should match the name of your composite unique constraint
            company_id,
            slug
          }
        },
        update: { slug, company_id, entity_id: masterEntityId },
        create: {
          slug,
          company_id,
          name: right,
          entity_id: masterEntityId,
          order_no
        }
      });
      order_no++;
    }
    console.log("Permission creation completed.");
  } catch (error) {
    console.log(`Permission seeder failed: ${permissionData.name}`);
    throw error;
  } finally {
  }
}
async function permissionsSeeder(company_id) {
  try {
    const permissions = JSON.parse(
      import_fs.default.readFileSync(__dirname + "/json/permission-seeds.json", "utf-8")
    );
    for (const permissionData of permissions) {
      await findOrCreatePermission(permissionData, company_id);
    }
    console.log("Permissions creation completed.");
  } catch (error) {
    throw error;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  permissionsSeeder
});
