"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 languageContent_seeds_exports = {};
__export(languageContent_seeds_exports, {
  languageContentSeeds: () => languageContentSeeds
});
module.exports = __toCommonJS(languageContent_seeds_exports);
var import_client = require("@prisma/pg/client");
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
const prisma = new import_client.PrismaClient();
function formatDisplayName(displayName) {
  return displayName.replace(/_/g, " ").toLowerCase().replace(/\b\w/g, (char) => char.toUpperCase());
}
async function languageContent(languageContentData, companyId, languageCode) {
  try {
    let user = await prisma.user.findFirst({
      where: { company_id: companyId, role: "super_admin" }
    });
    if (!user) {
      console.log(
        `Menu seeder failed: admin user not available for company id ${companyId}`
      );
      return false;
    }
    const createdBy = user.id;
    const company = await prisma.user.findFirst({
      where: { company_id: companyId }
    });
    if (!company) {
      console.log(
        `WARNING: languageSeeds: Company with id ${companyId} does not exist.`
      );
      return;
    }
    const language = await prisma.language.findFirst({
      where: {
        company_id: companyId,
        code: languageCode
      },
      select: {
        id: true
      }
    });
    if (!language) {
      console.log(
        `WARNING: languageContentSeeds: Language with code ${languageCode} does not exist for company id ${companyId}.`
      );
      return;
    }
    const languageId = language.id;
    const distinctEntityNames = await prisma.masterEntityLineItem.findMany({
      where: {
        company_id: companyId,
        status_id: 1
      },
      distinct: ["display_name"],
      select: {
        display_name: true
      }
    });
    const entityNames = distinctEntityNames.map((item) => item.display_name);
    const menuNames = await prisma.menus.findMany({
      where: {
        company_id: companyId,
        status_id: 1
      },
      select: {
        name: true
      }
    });
    const menuNameList = menuNames.map((item) => item.name);
    const menuItemNames = await prisma.menuItems.findMany({
      where: {
        company_id: companyId,
        status_id: 1
      },
      select: {
        name: true
      },
      orderBy: {
        order_no: "asc"
      }
    });
    const menuItemNameList = menuItemNames.map((item) => item.name);
    const displayNames = [...entityNames, ...menuNameList, ...menuItemNameList];
    const keyValueMapping = {};
    for (const displayName of displayNames) {
      const formattedValue = formatDisplayName(displayName);
      keyValueMapping[displayName] = formattedValue;
    }
    for (const [key, value] of Object.entries(languageContentData)) {
      keyValueMapping[key] = value;
    }
    for (const [key, value] of Object.entries(keyValueMapping)) {
      const existingContent = await prisma.languageContent.findFirst({
        where: {
          language_id: languageId,
          company_id: companyId,
          key_content: key
        }
      });
      if (!existingContent) {
        await prisma.languageContent.create({
          data: {
            language_id: languageId,
            key_content: key,
            values: value,
            company_id: companyId
          }
        });
        console.log(
          `Created new languageContent: key ${key}, value ${value}, company_id ${companyId}`
        );
      } else {
        console.log(
          `Skipped existing languageContent: key ${key}, company_id ${companyId}`
        );
      }
    }
  } catch (error) {
    console.error(`Error in languageContentSeeds: ${error}`);
    if (error instanceof import_client.Prisma.PrismaClientKnownRequestError && error.code === "P2003" && error.meta) {
      console.error(`Foreign key constraint failed: ${error.meta.field_name}`);
    }
    throw error;
  } finally {
  }
}
async function languageContentSeeds(company_id) {
  try {
    const filePathEn = import_path.default.join(
      __dirname,
      "/json/language-content-english.json"
    );
    const languageContentListEn = JSON.parse(
      import_fs.default.readFileSync(filePathEn, "utf-8")
    );
    for (const languageContentDataEn of languageContentListEn) {
      await languageContent(languageContentDataEn, company_id, "en-GB");
    }
    const filePathAr = import_path.default.join(
      __dirname,
      "/json/language-content-arabic.json"
    );
    const languageContentListAr = JSON.parse(
      import_fs.default.readFileSync(filePathAr, "utf-8")
    );
    for (const languageContentDataAr of languageContentListAr) {
      await languageContent(languageContentDataAr, company_id, "ar-QA");
    }
    console.log("language Content seeds completed.");
  } catch (error) {
    throw error;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  languageContentSeeds
});
