"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } 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 _class;

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

// src/core/registry/DataClassRegistry.ts
var DataClassRegistry = (_class = class {
  static {
    _chunk7QVYU63Ejs.__name.call(void 0, this, "DataClassRegistry");
  }
  static __initStatic() {this._map = /* @__PURE__ */ new Map()}
  static registerObjectClass(key, classConstructor) {
    const classKey = key.name;
    if (!this._map.has(classKey)) {
      this._map.set(classKey, classConstructor);
    }
  }
  static get(classKey) {
    const response = this._map.get(classKey.name);
    if (!response) {
      throw new Error(`Class not registered for key: ${typeof classKey === "string" ? classKey : classKey.name}`);
    }
    return response;
  }
  /**
   * Bootstrap the registry with all modules.
   * This is a convenience method for apps to register all their modules at once.
   *
   * @param modules - An object with module definitions (like the app's Modules class)
   */
  static bootstrap(modules) {
    Object.values(modules).forEach((module) => {
      if (module && module.model) {
        this.registerObjectClass(module, module.model);
      }
    });
  }
  /**
   * Clear all registered classes. Useful for testing.
   */
  static clear() {
    this._map.clear();
  }
}, _class.__initStatic(), _class);

// src/core/factories/JsonApiDataFactory.ts
var JsonApiDataFactory = class {
  static {
    _chunk7QVYU63Ejs.__name.call(void 0, this, "JsonApiDataFactory");
  }
  static create(classKey, data) {
    const factoryClass = DataClassRegistry.get(classKey);
    const instance = new factoryClass();
    return instance.createJsonApi(data);
  }
};

// src/core/registry/bootstrapStore.ts
var BOOTSTRAP_KEY = /* @__PURE__ */ Symbol.for("nextjs-jsonapi:bootstrapper");
var BOOTSTRAP_ATTEMPTED_KEY = /* @__PURE__ */ Symbol.for("nextjs-jsonapi:bootstrapAttempted");
var globalStore = globalThis;
if (globalStore[BOOTSTRAP_KEY] === void 0) {
  globalStore[BOOTSTRAP_KEY] = null;
}
if (globalStore[BOOTSTRAP_ATTEMPTED_KEY] === void 0) {
  globalStore[BOOTSTRAP_ATTEMPTED_KEY] = false;
}
function getBootstrapperInternal() {
  return _nullishCoalesce(globalStore[BOOTSTRAP_KEY], () => ( null));
}
_chunk7QVYU63Ejs.__name.call(void 0, getBootstrapperInternal, "getBootstrapperInternal");
function setBootstrapperInternal(fn) {
  globalStore[BOOTSTRAP_KEY] = fn;
}
_chunk7QVYU63Ejs.__name.call(void 0, setBootstrapperInternal, "setBootstrapperInternal");
function getBootstrapAttempted() {
  return _nullishCoalesce(globalStore[BOOTSTRAP_ATTEMPTED_KEY], () => ( false));
}
_chunk7QVYU63Ejs.__name.call(void 0, getBootstrapAttempted, "getBootstrapAttempted");
function setBootstrapAttempted(value) {
  globalStore[BOOTSTRAP_ATTEMPTED_KEY] = value;
}
_chunk7QVYU63Ejs.__name.call(void 0, setBootstrapAttempted, "setBootstrapAttempted");
function setBootstrapper(fn) {
  setBootstrapperInternal(fn);
}
_chunk7QVYU63Ejs.__name.call(void 0, setBootstrapper, "setBootstrapper");
function getBootstrapper() {
  return getBootstrapperInternal();
}
_chunk7QVYU63Ejs.__name.call(void 0, getBootstrapper, "getBootstrapper");
function tryBootstrap() {
  const bootstrapper = getBootstrapperInternal();
  const attempted = getBootstrapAttempted();
  if (attempted && !bootstrapper) {
    return false;
  }
  setBootstrapAttempted(true);
  if (bootstrapper) {
    bootstrapper();
    return true;
  }
  return false;
}
_chunk7QVYU63Ejs.__name.call(void 0, tryBootstrap, "tryBootstrap");
function hasBootstrapper() {
  return getBootstrapperInternal() !== null;
}
_chunk7QVYU63Ejs.__name.call(void 0, hasBootstrapper, "hasBootstrapper");
function resetBootstrapStore() {
  setBootstrapperInternal(null);
  setBootstrapAttempted(false);
}
_chunk7QVYU63Ejs.__name.call(void 0, resetBootstrapStore, "resetBootstrapStore");

// src/core/utils/translateResponse.ts
function translateData(params) {
  const factoryClass = DataClassRegistry.get(params.classKey);
  if (!factoryClass) {
    throw new Error(
      `Class not registered for key: ${typeof params.classKey === "string" ? params.classKey : params.classKey.name}`
    );
  }
  const included = _nullishCoalesce(params.data.included, () => ( []));
  if (Array.isArray(params.data.data)) {
    const responseData = [];
    for (const data of params.data.data) {
      const object = new factoryClass();
      object.rehydrate({ jsonApi: data, included, allData: params.data.data });
      responseData.push(object);
    }
    return responseData;
  } else {
    const responseData = new factoryClass();
    responseData.rehydrate({
      jsonApi: params.data.data,
      included
    });
    return responseData;
  }
}
_chunk7QVYU63Ejs.__name.call(void 0, translateData, "translateData");
async function translateResponse(params) {
  const response = {
    ok: true,
    response: 0,
    data: [],
    error: ""
  };
  const factoryClass = DataClassRegistry.get(params.classKey);
  if (!factoryClass) {
    throw new Error(
      `Class not registered for key: ${typeof params.classKey === "string" ? params.classKey : params.classKey.name}`
    );
  }
  response.ok = params.apiResponse.ok;
  response.response = params.apiResponse.status;
  if (!params.apiResponse.ok) {
    response.error = _nullishCoalesce(_optionalChain([params, 'access', _ => _.apiResponse, 'optionalAccess', _2 => _2.data, 'optionalAccess', _3 => _3.message]), () => ( params.apiResponse.statusText));
    return response;
  }
  if (params.apiResponse.status === 204) return response;
  response.raw = params.apiResponse.data;
  if (_optionalChain([params, 'access', _4 => _4.apiResponse, 'access', _5 => _5.data, 'optionalAccess', _6 => _6.meta])) {
    response.meta = params.apiResponse.data.meta;
  }
  try {
    if (params.apiResponse.data && typeof params.apiResponse.data === "object" && !Array.isArray(params.apiResponse.data) && params.apiResponse.data.data === void 0) {
      response.data = params.apiResponse.data;
      return response;
    }
    const included = _nullishCoalesce(params.apiResponse.data.included, () => ( []));
    if (params.apiResponse.data.links) {
      response.self = params.apiResponse.data.links.self;
      if (params.apiResponse.data.links.next && params.paginationHandler) {
        response.next = params.apiResponse.data.links.next;
        response.nextPage = async () => params.paginationHandler(params.apiResponse.data.links.next);
      }
      if (params.apiResponse.data.links.prev && params.paginationHandler) {
        response.prev = params.apiResponse.data.links.prev;
        response.prevPage = async () => params.paginationHandler(params.apiResponse.data.links.prev);
      }
    }
    if (Array.isArray(params.apiResponse.data.data)) {
      const responseData = [];
      for (const data of params.apiResponse.data.data) {
        const object = new factoryClass();
        object.rehydrate({ jsonApi: data, included, allData: params.apiResponse.data.data });
        responseData.push(object);
      }
      response.data = responseData;
    } else {
      const responseData = new factoryClass();
      responseData.rehydrate({
        jsonApi: params.apiResponse.data.data,
        included
      });
      response.data = responseData;
    }
  } catch (e) {
    console.error(e);
  }
  return response;
}
_chunk7QVYU63Ejs.__name.call(void 0, translateResponse, "translateResponse");

// src/unified/JsonApiRequest.ts
var _directFetch;
var _serverRequest;
var _getClientToken;
var _getServerToken;
var _staticConfig = null;
function configureJsonApi(config) {
  _staticConfig = config;
  if (config.bootstrapper) {
    setBootstrapper(config.bootstrapper);
    config.bootstrapper();
  }
}
_chunk7QVYU63Ejs.__name.call(void 0, configureJsonApi, "configureJsonApi");
async function getToken() {
  if (typeof window === "undefined") {
    if (!_getServerToken) {
      const serverModule = await Promise.resolve().then(() => _interopRequireWildcard(require("./token-2UWQJY5T.js")));
      _getServerToken = serverModule.getServerToken;
    }
    return _getServerToken();
  } else {
    if (!_getClientToken) {
      const clientModule = await Promise.resolve().then(() => _interopRequireWildcard(require("./token-MJMC26ON.js")));
      _getClientToken = clientModule.getClientToken;
    }
    return _getClientToken();
  }
}
_chunk7QVYU63Ejs.__name.call(void 0, getToken, "getToken");
async function makeRequest(params) {
  if (typeof window !== "undefined") {
    if (!_directFetch) {
      const clientModule = await Promise.resolve().then(() => _interopRequireWildcard(require("./request-ZYY6RI5X.js")));
      _directFetch = clientModule.directFetch;
    }
    return _directFetch({
      method: params.method,
      url: params.url,
      token: params.token,
      body: params.body,
      files: params.files,
      companyId: params.companyId,
      language: params.language,
      additionalHeaders: params.additionalHeaders
    });
  } else {
    if (!_serverRequest) {
      const serverModule = await Promise.resolve().then(() => _interopRequireWildcard(require("./request-GBLBPYFM.js")));
      _serverRequest = serverModule.serverRequest;
    }
    return _serverRequest({
      method: params.method,
      url: params.url,
      token: params.token,
      cache: params.cache,
      body: params.body,
      files: params.files,
      companyId: params.companyId,
      language: params.language,
      additionalHeaders: params.additionalHeaders
    });
  }
}
_chunk7QVYU63Ejs.__name.call(void 0, makeRequest, "makeRequest");
function getApiUrl() {
  if (_optionalChain([_staticConfig, 'optionalAccess', _7 => _7.apiUrl])) {
    return _staticConfig.apiUrl;
  }
  const envUrl = process.env.NEXT_PUBLIC_API_URL;
  if (!envUrl) {
    throw new Error("API URL not configured. Use configureJsonApi() or set NEXT_PUBLIC_API_URL environment variable.");
  }
  return envUrl;
}
_chunk7QVYU63Ejs.__name.call(void 0, getApiUrl, "getApiUrl");
function getAppUrl() {
  if (_optionalChain([_staticConfig, 'optionalAccess', _8 => _8.appUrl])) {
    return _staticConfig.appUrl;
  }
  const envUrl = process.env.NEXT_PUBLIC_ADDRESS;
  if (!envUrl) {
    throw new Error(
      "App URL not configured. Use configureJsonApi({ appUrl }) or set NEXT_PUBLIC_ADDRESS environment variable."
    );
  }
  return envUrl.trim().replace(/\/+$/, "");
}
_chunk7QVYU63Ejs.__name.call(void 0, getAppUrl, "getAppUrl");
function getTrackablePages() {
  return _nullishCoalesce(_optionalChain([_staticConfig, 'optionalAccess', _9 => _9.trackablePages]), () => ( []));
}
_chunk7QVYU63Ejs.__name.call(void 0, getTrackablePages, "getTrackablePages");
function runBootstrapper() {
  if (_optionalChain([_staticConfig, 'optionalAccess', _10 => _10.bootstrapper])) {
    _staticConfig.bootstrapper();
  }
}
_chunk7QVYU63Ejs.__name.call(void 0, runBootstrapper, "runBootstrapper");
function buildUrl(endpoint) {
  const apiUrl = getApiUrl();
  return endpoint.startsWith("http") ? endpoint : `${apiUrl}${endpoint}`;
}
_chunk7QVYU63Ejs.__name.call(void 0, buildUrl, "buildUrl");
async function JsonApiGet(params) {
  runBootstrapper();
  const token = await getToken();
  const apiResponse = await makeRequest({
    method: "GET",
    url: buildUrl(params.endpoint),
    token,
    cache: params.classKey.cache,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_staticConfig, 'optionalAccess', _11 => _11.additionalHeaders])
  });
  return translateResponse({
    classKey: params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language,
    paginationHandler: /* @__PURE__ */ _chunk7QVYU63Ejs.__name.call(void 0, async (endpoint) => JsonApiGet({
      classKey: params.classKey,
      endpoint,
      companyId: params.companyId,
      language: params.language
    }), "paginationHandler")
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, JsonApiGet, "JsonApiGet");
async function JsonApiPost(params) {
  runBootstrapper();
  const token = await getToken();
  let body = params.body;
  if (!body) {
    body = {};
  } else if (params.overridesJsonApiCreation !== true) {
    body = JsonApiDataFactory.create(params.classKey, body);
  }
  const apiResponse = await makeRequest({
    method: "POST",
    url: buildUrl(params.endpoint),
    token,
    body,
    files: params.files,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_staticConfig, 'optionalAccess', _12 => _12.additionalHeaders])
  });
  return translateResponse({
    classKey: _nullishCoalesce(params.responseType, () => ( params.classKey)),
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, JsonApiPost, "JsonApiPost");
async function JsonApiPut(params) {
  runBootstrapper();
  const token = await getToken();
  let body = params.body;
  if (!body) {
    body = {};
  } else {
    body = JsonApiDataFactory.create(params.classKey, body);
  }
  const apiResponse = await makeRequest({
    method: "PUT",
    url: buildUrl(params.endpoint),
    token,
    body,
    files: params.files,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_staticConfig, 'optionalAccess', _13 => _13.additionalHeaders])
  });
  return translateResponse({
    classKey: _nullishCoalesce(params.responseType, () => ( params.classKey)),
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, JsonApiPut, "JsonApiPut");
async function JsonApiPatch(params) {
  runBootstrapper();
  const token = await getToken();
  let body = params.body;
  if (!body) {
    body = {};
  } else if (params.overridesJsonApiCreation !== true) {
    body = JsonApiDataFactory.create(params.classKey, body);
  }
  const apiResponse = await makeRequest({
    method: "PATCH",
    url: buildUrl(params.endpoint),
    token,
    body,
    files: params.files,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_staticConfig, 'optionalAccess', _14 => _14.additionalHeaders])
  });
  return translateResponse({
    classKey: _nullishCoalesce(params.responseType, () => ( params.classKey)),
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, JsonApiPatch, "JsonApiPatch");
async function JsonApiDelete(params) {
  runBootstrapper();
  const token = await getToken();
  const apiResponse = await makeRequest({
    method: "DELETE",
    url: buildUrl(params.endpoint),
    token,
    companyId: params.companyId,
    language: params.language,
    additionalHeaders: _optionalChain([_staticConfig, 'optionalAccess', _15 => _15.additionalHeaders])
  });
  return translateResponse({
    classKey: _nullishCoalesce(params.responseType, () => ( params.classKey)),
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
_chunk7QVYU63Ejs.__name.call(void 0, JsonApiDelete, "JsonApiDelete");




















exports.DataClassRegistry = DataClassRegistry; exports.JsonApiDataFactory = JsonApiDataFactory; exports.setBootstrapper = setBootstrapper; exports.getBootstrapper = getBootstrapper; exports.tryBootstrap = tryBootstrap; exports.hasBootstrapper = hasBootstrapper; exports.resetBootstrapStore = resetBootstrapStore; exports.translateData = translateData; exports.translateResponse = translateResponse; exports.configureJsonApi = configureJsonApi; exports.getApiUrl = getApiUrl; exports.getAppUrl = getAppUrl; exports.getTrackablePages = getTrackablePages; exports.JsonApiGet = JsonApiGet; exports.JsonApiPost = JsonApiPost; exports.JsonApiPut = JsonApiPut; exports.JsonApiPatch = JsonApiPatch; exports.JsonApiDelete = JsonApiDelete;
//# sourceMappingURL=chunk-LI6CPNJI.js.map