"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }

var _chunkYUO55Q5Ajs = require('../chunk-YUO55Q5A.js');


var _chunk3ZPK4QOBjs = require('../chunk-3ZPK4QOB.js');











var _chunkZNGEVB5Mjs = require('../chunk-ZNGEVB5M.js');
require('../chunk-LXKSUWAV.js');
require('../chunk-IBS6NI7D.js');



var _chunkFM6WRAN5js = require('../chunk-FM6WRAN5.js');


var _chunk7QVYU63Ejs = require('../chunk-7QVYU63E.js');

// src/server/cache.ts
var _cache = require('next/cache');
async function invalidateCacheTag(tag, profile = "max") {
  _cache.revalidateTag.call(void 0, tag, profile);
}
_chunk7QVYU63Ejs.__name.call(void 0, invalidateCacheTag, "invalidateCacheTag");
async function invalidateCacheTags(tags, profile = "max") {
  for (const tag of tags) {
    _cache.revalidateTag.call(void 0, tag, profile);
  }
}
_chunk7QVYU63Ejs.__name.call(void 0, invalidateCacheTags, "invalidateCacheTags");

// src/server/ServerSession.ts
var _headers = require('next/headers');
var _navigation = require('next/navigation');
var _pako = require('pako'); var _pako2 = _interopRequireDefault(_pako);
var ServerSession = class {
  static {
    _chunk7QVYU63Ejs.__name.call(void 0, this, "ServerSession");
  }
  static async isLogged() {
    const cookieStore = await _headers.cookies.call(void 0, );
    const token = cookieStore.get("token");
    if (!token || !token.value) return false;
    return true;
  }
  static async companyId() {
    const cookieStore = await _headers.cookies.call(void 0, );
    return _nullishCoalesce(_optionalChain([cookieStore, 'access', _ => _.get, 'call', _2 => _2("companyId"), 'optionalAccess', _3 => _3.value]), () => ( ""));
  }
  static async userId() {
    const cookieStore = await _headers.cookies.call(void 0, );
    return _nullishCoalesce(_optionalChain([cookieStore, 'access', _4 => _4.get, 'call', _5 => _5("userId"), 'optionalAccess', _6 => _6.value]), () => ( ""));
  }
  static async checkPermission(params) {
    if (!await this.hasPermissionToModule(params)) _navigation.redirect.call(void 0, `/401`);
  }
  static async hasRole(roleId) {
    const cookieStore = await _headers.cookies.call(void 0, );
    const roles = _optionalChain([cookieStore, 'access', _7 => _7.get, 'call', _8 => _8("roles"), 'optionalAccess', _9 => _9.value]);
    if (!roles || !roles.includes(roleId)) return false;
    return true;
  }
  static async hasFeature(featureId) {
    const cookieStore = await _headers.cookies.call(void 0, );
    const features = _optionalChain([cookieStore, 'access', _10 => _10.get, 'call', _11 => _11("features"), 'optionalAccess', _12 => _12.value]);
    if (!features || !features.includes(featureId)) return false;
    return true;
  }
  static async hasPermissionToModule(params) {
    const cookieStore = await _headers.cookies.call(void 0, );
    if (params.module.feature) {
      const features = _optionalChain([cookieStore, 'access', _13 => _13.get, 'call', _14 => _14("features"), 'optionalAccess', _15 => _15.value]);
      if (features && !features.includes(params.module.feature)) return false;
    }
    const rawModules = _optionalChain([cookieStore, 'access', _16 => _16.get, 'call', _17 => _17("modules"), 'optionalAccess', _18 => _18.value]);
    if (!rawModules) return false;
    const modules = JSON.parse(_pako2.default.ungzip(Buffer.from(rawModules, "base64"), { to: "string" }));
    const selectedModule = modules.find((module) => module.id === params.module.moduleId);
    return _chunkZNGEVB5Mjs.checkPermissionsFromServer.call(void 0, {
      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();
  }
}
_chunk7QVYU63Ejs.__name.call(void 0, configureServerJsonApi, "configureServerJsonApi");
function getServerApiUrl() {
  if (_optionalChain([_serverConfig, 'optionalAccess', _19 => _19.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;
}
_chunk7QVYU63Ejs.__name.call(void 0, getServerApiUrl, "getServerApiUrl");
function getServerAppUrl() {
  if (_optionalChain([_serverConfig, 'optionalAccess', _20 => _20.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(/\/+$/, "");
}
_chunk7QVYU63Ejs.__name.call(void 0, getServerAppUrl, "getServerAppUrl");
function getServerTrackablePages() {
  return _nullishCoalesce(_optionalChain([_serverConfig, 'optionalAccess', _21 => _21.trackablePages]), () => ( []));
}
_chunk7QVYU63Ejs.__name.call(void 0, getServerTrackablePages, "getServerTrackablePages");
function runServerBootstrapper() {
  if (_optionalChain([_serverConfig, 'optionalAccess', _22 => _22.bootstrapper])) {
    _serverConfig.bootstrapper();
  }
}
_chunk7QVYU63Ejs.__name.call(void 0, runServerBootstrapper, "runServerBootstrapper");
function buildServerUrl(endpoint) {
  const apiUrl = getServerApiUrl();
  return endpoint.startsWith("http") ? endpoint : `${apiUrl}${endpoint}`;
}
_chunk7QVYU63Ejs.__name.call(void 0, buildServerUrl, "buildServerUrl");
async function ServerJsonApiGet(params) {
  runServerBootstrapper();
  const token = await _chunkYUO55Q5Ajs.getServerToken.call(void 0, );
  const apiResponse = await _chunk3ZPK4QOBjs.serverRequest.call(void 0, {
    method: "GET",
    url: buildServerUrl(params.endpoint),
    token,
    cache: params.classKey.cache,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_serverConfig, 'optionalAccess', _23 => _23.additionalHeaders])
  });
  return _chunkFM6WRAN5js.translateResponse.call(void 0, {
    classKey: params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language,
    paginationHandler: /* @__PURE__ */ _chunk7QVYU63Ejs.__name.call(void 0, async (endpoint) => ServerJsonApiGet({
      classKey: params.classKey,
      endpoint,
      companyId: params.companyId,
      language: params.language
    }), "paginationHandler")
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, ServerJsonApiGet, "ServerJsonApiGet");
async function ServerJsonApiPost(params) {
  runServerBootstrapper();
  const token = await _chunkYUO55Q5Ajs.getServerToken.call(void 0, );
  let body = params.body;
  if (!body) {
    body = {};
  } else if (params.overridesJsonApiCreation !== true) {
    body = _chunkFM6WRAN5js.JsonApiDataFactory.create(params.classKey, body);
  }
  const apiResponse = await _chunk3ZPK4QOBjs.serverRequest.call(void 0, {
    method: "POST",
    url: buildServerUrl(params.endpoint),
    token,
    body,
    files: params.files,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_serverConfig, 'optionalAccess', _24 => _24.additionalHeaders])
  });
  return _chunkFM6WRAN5js.translateResponse.call(void 0, {
    classKey: _nullishCoalesce(params.responseType, () => ( params.classKey)),
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, ServerJsonApiPost, "ServerJsonApiPost");
async function ServerJsonApiPut(params) {
  runServerBootstrapper();
  const token = await _chunkYUO55Q5Ajs.getServerToken.call(void 0, );
  let body = params.body;
  if (!body) {
    body = {};
  } else {
    body = _chunkFM6WRAN5js.JsonApiDataFactory.create(params.classKey, body);
  }
  const apiResponse = await _chunk3ZPK4QOBjs.serverRequest.call(void 0, {
    method: "PUT",
    url: buildServerUrl(params.endpoint),
    token,
    body,
    files: params.files,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_serverConfig, 'optionalAccess', _25 => _25.additionalHeaders])
  });
  return _chunkFM6WRAN5js.translateResponse.call(void 0, {
    classKey: _nullishCoalesce(params.responseType, () => ( params.classKey)),
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, ServerJsonApiPut, "ServerJsonApiPut");
async function ServerJsonApiPatch(params) {
  runServerBootstrapper();
  const token = await _chunkYUO55Q5Ajs.getServerToken.call(void 0, );
  let body = params.body;
  if (!body) {
    body = {};
  } else if (params.overridesJsonApiCreation !== true) {
    body = _chunkFM6WRAN5js.JsonApiDataFactory.create(params.classKey, body);
  }
  const apiResponse = await _chunk3ZPK4QOBjs.serverRequest.call(void 0, {
    method: "PATCH",
    url: buildServerUrl(params.endpoint),
    token,
    body,
    files: params.files,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_serverConfig, 'optionalAccess', _26 => _26.additionalHeaders])
  });
  return _chunkFM6WRAN5js.translateResponse.call(void 0, {
    classKey: _nullishCoalesce(params.responseType, () => ( params.classKey)),
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, ServerJsonApiPatch, "ServerJsonApiPatch");
async function ServerJsonApiDelete(params) {
  runServerBootstrapper();
  const token = await _chunkYUO55Q5Ajs.getServerToken.call(void 0, );
  const apiResponse = await _chunk3ZPK4QOBjs.serverRequest.call(void 0, {
    method: "DELETE",
    url: buildServerUrl(params.endpoint),
    token,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_serverConfig, 'optionalAccess', _27 => _27.additionalHeaders])
  });
  return _chunkFM6WRAN5js.translateResponse.call(void 0, {
    classKey: _nullishCoalesce(params.responseType, () => ( params.classKey)),
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, ServerJsonApiDelete, "ServerJsonApiDelete");
























exports.ServerAuthService = _chunkZNGEVB5Mjs.AuthService; exports.ServerCompanyService = _chunkZNGEVB5Mjs.CompanyService; exports.ServerContentService = _chunkZNGEVB5Mjs.ContentService; exports.ServerFeatureService = _chunkZNGEVB5Mjs.FeatureService; exports.ServerJsonApiDelete = ServerJsonApiDelete; exports.ServerJsonApiGet = ServerJsonApiGet; exports.ServerJsonApiPatch = ServerJsonApiPatch; exports.ServerJsonApiPost = ServerJsonApiPost; exports.ServerJsonApiPut = ServerJsonApiPut; exports.ServerNotificationService = _chunkZNGEVB5Mjs.NotificationService; exports.ServerPushService = _chunkZNGEVB5Mjs.PushService; exports.ServerRoleService = _chunkZNGEVB5Mjs.RoleService; exports.ServerS3Service = _chunkZNGEVB5Mjs.S3Service; exports.ServerSession = ServerSession; exports.ServerUserService = _chunkZNGEVB5Mjs.UserService; exports.configureServerJsonApi = configureServerJsonApi; exports.getServerApiUrl = getServerApiUrl; exports.getServerAppUrl = getServerAppUrl; exports.getServerToken = _chunkYUO55Q5Ajs.getServerToken; exports.getServerTrackablePages = getServerTrackablePages; exports.invalidateCacheTag = invalidateCacheTag; exports.invalidateCacheTags = invalidateCacheTags; exports.serverRequest = _chunk3ZPK4QOBjs.serverRequest;
//# sourceMappingURL=index.js.map