import {
  getServerToken
} from "../chunk-F5UNXZ3J.mjs";
import {
  serverRequest
} from "../chunk-KJ4ETLJB.mjs";
import {
  AuthService,
  CompanyService,
  ContentService,
  FeatureService,
  NotificationService,
  PushService,
  RoleService,
  S3Service,
  UserService,
  checkPermissionsFromServer
} from "../chunk-GVN7XC3U.mjs";
import "../chunk-AUXK7QSA.mjs";
import "../chunk-C7C7VY4F.mjs";
import {
  JsonApiDataFactory,
  translateResponse
} from "../chunk-U4MTVHOC.mjs";
import {
  __name
} from "../chunk-PAWJFY3S.mjs";

// src/server/cache.ts
import { revalidateTag } from "next/cache";
async function invalidateCacheTag(tag, profile = "max") {
  revalidateTag(tag, profile);
}
__name(invalidateCacheTag, "invalidateCacheTag");
async function invalidateCacheTags(tags, profile = "max") {
  for (const tag of tags) {
    revalidateTag(tag, profile);
  }
}
__name(invalidateCacheTags, "invalidateCacheTags");

// src/server/ServerSession.ts
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import pako from "pako";
var ServerSession = class {
  static {
    __name(this, "ServerSession");
  }
  static async isLogged() {
    const cookieStore = await cookies();
    const token = cookieStore.get("token");
    if (!token || !token.value) return false;
    return true;
  }
  static async companyId() {
    const cookieStore = await cookies();
    return cookieStore.get("companyId")?.value ?? "";
  }
  static async userId() {
    const cookieStore = await cookies();
    return cookieStore.get("userId")?.value ?? "";
  }
  static async checkPermission(params) {
    if (!await this.hasPermissionToModule(params)) redirect(`/401`);
  }
  static async hasRole(roleId) {
    const cookieStore = await cookies();
    const roles = cookieStore.get("roles")?.value;
    if (!roles || !roles.includes(roleId)) return false;
    return true;
  }
  static async hasFeature(featureId) {
    const cookieStore = await cookies();
    const features = cookieStore.get("features")?.value;
    if (!features || !features.includes(featureId)) return false;
    return true;
  }
  static async hasPermissionToModule(params) {
    const cookieStore = await cookies();
    if (params.module.feature) {
      const features = cookieStore.get("features")?.value;
      if (features && !features.includes(params.module.feature)) return false;
    }
    const rawModules = cookieStore.get("modules")?.value;
    if (!rawModules) return false;
    const modules = JSON.parse(pako.ungzip(Buffer.from(rawModules, "base64"), { to: "string" }));
    const selectedModule = modules.find((module) => module.id === params.module.moduleId);
    return checkPermissionsFromServer({
      module: params.module,
      action: params.action,
      data: params.data,
      userId: await this.userId(),
      selectedModule
    });
  }
};

// src/server/JsonApiServer.ts
var _serverConfig = null;
function configureServerJsonApi(config) {
  _serverConfig = config;
  if (config.bootstrapper) {
    config.bootstrapper();
  }
}
__name(configureServerJsonApi, "configureServerJsonApi");
function getServerApiUrl() {
  if (_serverConfig?.apiUrl) {
    return _serverConfig.apiUrl;
  }
  const envUrl = process.env.NEXT_PUBLIC_API_URL;
  if (!envUrl) {
    throw new Error(
      "API URL not configured. Use configureServerJsonApi() or set NEXT_PUBLIC_API_URL environment variable."
    );
  }
  return envUrl;
}
__name(getServerApiUrl, "getServerApiUrl");
function getServerAppUrl() {
  if (_serverConfig?.appUrl) {
    return _serverConfig.appUrl;
  }
  const envUrl = process.env.NEXT_PUBLIC_ADDRESS;
  if (!envUrl) {
    throw new Error(
      "App URL not configured. Use configureServerJsonApi({ appUrl }) or set NEXT_PUBLIC_ADDRESS environment variable."
    );
  }
  return envUrl.trim().replace(/\/+$/, "");
}
__name(getServerAppUrl, "getServerAppUrl");
function getServerTrackablePages() {
  return _serverConfig?.trackablePages ?? [];
}
__name(getServerTrackablePages, "getServerTrackablePages");
function runServerBootstrapper() {
  if (_serverConfig?.bootstrapper) {
    _serverConfig.bootstrapper();
  }
}
__name(runServerBootstrapper, "runServerBootstrapper");
function buildServerUrl(endpoint) {
  const apiUrl = getServerApiUrl();
  return endpoint.startsWith("http") ? endpoint : `${apiUrl}${endpoint}`;
}
__name(buildServerUrl, "buildServerUrl");
async function ServerJsonApiGet(params) {
  runServerBootstrapper();
  const token = await getServerToken();
  const apiResponse = await serverRequest({
    method: "GET",
    url: buildServerUrl(params.endpoint),
    token,
    cache: params.classKey.cache,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _serverConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language,
    paginationHandler: /* @__PURE__ */ __name(async (endpoint) => ServerJsonApiGet({
      classKey: params.classKey,
      endpoint,
      companyId: params.companyId,
      language: params.language
    }), "paginationHandler")
  });
}
__name(ServerJsonApiGet, "ServerJsonApiGet");
async function ServerJsonApiPost(params) {
  runServerBootstrapper();
  const token = await getServerToken();
  let body = params.body;
  if (!body) {
    body = {};
  } else if (params.overridesJsonApiCreation !== true) {
    body = JsonApiDataFactory.create(params.classKey, body);
  }
  const apiResponse = await serverRequest({
    method: "POST",
    url: buildServerUrl(params.endpoint),
    token,
    body,
    files: params.files,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _serverConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.responseType ?? params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
__name(ServerJsonApiPost, "ServerJsonApiPost");
async function ServerJsonApiPut(params) {
  runServerBootstrapper();
  const token = await getServerToken();
  let body = params.body;
  if (!body) {
    body = {};
  } else {
    body = JsonApiDataFactory.create(params.classKey, body);
  }
  const apiResponse = await serverRequest({
    method: "PUT",
    url: buildServerUrl(params.endpoint),
    token,
    body,
    files: params.files,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _serverConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.responseType ?? params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
__name(ServerJsonApiPut, "ServerJsonApiPut");
async function ServerJsonApiPatch(params) {
  runServerBootstrapper();
  const token = await getServerToken();
  let body = params.body;
  if (!body) {
    body = {};
  } else if (params.overridesJsonApiCreation !== true) {
    body = JsonApiDataFactory.create(params.classKey, body);
  }
  const apiResponse = await serverRequest({
    method: "PATCH",
    url: buildServerUrl(params.endpoint),
    token,
    body,
    files: params.files,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _serverConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.responseType ?? params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
__name(ServerJsonApiPatch, "ServerJsonApiPatch");
async function ServerJsonApiDelete(params) {
  runServerBootstrapper();
  const token = await getServerToken();
  const apiResponse = await serverRequest({
    method: "DELETE",
    url: buildServerUrl(params.endpoint),
    token,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _serverConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.responseType ?? params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
__name(ServerJsonApiDelete, "ServerJsonApiDelete");
export {
  AuthService as ServerAuthService,
  CompanyService as ServerCompanyService,
  ContentService as ServerContentService,
  FeatureService as ServerFeatureService,
  ServerJsonApiDelete,
  ServerJsonApiGet,
  ServerJsonApiPatch,
  ServerJsonApiPost,
  ServerJsonApiPut,
  NotificationService as ServerNotificationService,
  PushService as ServerPushService,
  RoleService as ServerRoleService,
  S3Service as ServerS3Service,
  ServerSession,
  UserService as ServerUserService,
  configureServerJsonApi,
  getServerApiUrl,
  getServerAppUrl,
  getServerToken,
  getServerTrackablePages,
  invalidateCacheTag,
  invalidateCacheTags,
  serverRequest
};
//# sourceMappingURL=index.mjs.map