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 dbAuthEncoder_exports = {};
__export(dbAuthEncoder_exports, {
  getDBAuthHeader: () => getDBAuthHeader
});
module.exports = __toCommonJS(dbAuthEncoder_exports);
var import_parse_int = __toESM(require("@babel/runtime-corejs3/core-js/parse-int"));
var import_stringify = __toESM(require("@babel/runtime-corejs3/core-js/json/stringify"));
var import_node_fs = __toESM(require("node:fs"));
var import_node_path = __toESM(require("node:path"));
var import_uuid = require("uuid");
var import_project = require("../../util/project");
const isNumeric = (id) => {
  return /^\d+$/.test(id);
};
const getDBAuthHeader = async (userId) => {
  if (!userId) {
    throw new Error("Require a unique id to generate session cookie");
  }
  if (!process.env.SESSION_SECRET) {
    throw new Error("dbAuth requires a SESSION_SECRET environment variable that is used to encrypt session cookies. Use `yarn rw g secret` to create one, then add to your `.env` file. DO NOT check this variable in to your version control system!!");
  }
  const {
    default: {
      encryptSession
    }
  } = await import("@cedarjs/auth-dbauth-api");
  const id = isNumeric(userId) ? (0, import_parse_int.default)(userId) : userId;
  const cookieName = await getCookieName();
  const cookieValue = encryptSession((0, import_stringify.default)({
    id
  }) + ";" + (0, import_uuid.v4)());
  return {
    authProvider: "dbAuth",
    cookie: `${cookieName}=${cookieValue}`,
    authorization: `Bearer ${userId}`
  };
};
async function getCookieName() {
  const distAuthPath = import_node_path.default.join((0, import_project.getUserProjectPaths)().api.dist, "lib", "auth.js");
  if (!import_node_fs.default.existsSync(distAuthPath)) {
    throw new Error("No auth.js file found in api/dist/lib in the user's project");
  }
  const projectCookieName = (await import(distAuthPath))?.cookieName;
  const projectApiPort = (0, import_project.getUserProjectConfig)().api.port;
  const cookieName = projectCookieName?.replace("%port%", "" + projectApiPort) ?? "session";
  return cookieName;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getDBAuthHeader
});
//# sourceMappingURL=dbAuthEncoder.js.map
