import {
  __name
} from "./chunk-PAWJFY3S.mjs";

// src/core/registry/DataClassRegistry.ts
var DataClassRegistry = class {
  static {
    __name(this, "DataClassRegistry");
  }
  static _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();
  }
};

// src/core/factories/JsonApiDataFactory.ts
var JsonApiDataFactory = class {
  static {
    __name(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 globalStore[BOOTSTRAP_KEY] ?? null;
}
__name(getBootstrapperInternal, "getBootstrapperInternal");
function setBootstrapperInternal(fn) {
  globalStore[BOOTSTRAP_KEY] = fn;
}
__name(setBootstrapperInternal, "setBootstrapperInternal");
function getBootstrapAttempted() {
  return globalStore[BOOTSTRAP_ATTEMPTED_KEY] ?? false;
}
__name(getBootstrapAttempted, "getBootstrapAttempted");
function setBootstrapAttempted(value) {
  globalStore[BOOTSTRAP_ATTEMPTED_KEY] = value;
}
__name(setBootstrapAttempted, "setBootstrapAttempted");
function setBootstrapper(fn) {
  setBootstrapperInternal(fn);
}
__name(setBootstrapper, "setBootstrapper");
function getBootstrapper() {
  return getBootstrapperInternal();
}
__name(getBootstrapper, "getBootstrapper");
function tryBootstrap() {
  const bootstrapper = getBootstrapperInternal();
  const attempted = getBootstrapAttempted();
  if (attempted && !bootstrapper) {
    return false;
  }
  setBootstrapAttempted(true);
  if (bootstrapper) {
    bootstrapper();
    return true;
  }
  return false;
}
__name(tryBootstrap, "tryBootstrap");
function hasBootstrapper() {
  return getBootstrapperInternal() !== null;
}
__name(hasBootstrapper, "hasBootstrapper");
function resetBootstrapStore() {
  setBootstrapperInternal(null);
  setBootstrapAttempted(false);
}
__name(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 = 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;
  }
}
__name(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 = params.apiResponse?.data?.message ?? params.apiResponse.statusText;
    return response;
  }
  if (params.apiResponse.status === 204) return response;
  response.raw = params.apiResponse.data;
  if (params.apiResponse.data?.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 = 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;
}
__name(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();
  }
}
__name(configureJsonApi, "configureJsonApi");
async function getToken() {
  if (typeof window === "undefined") {
    if (!_getServerToken) {
      const serverModule = await import("./token-EOK3N45S.mjs");
      _getServerToken = serverModule.getServerToken;
    }
    return _getServerToken();
  } else {
    if (!_getClientToken) {
      const clientModule = await import("./token-UADJQ7VC.mjs");
      _getClientToken = clientModule.getClientToken;
    }
    return _getClientToken();
  }
}
__name(getToken, "getToken");
async function makeRequest(params) {
  if (typeof window !== "undefined") {
    if (!_directFetch) {
      const clientModule = await import("./request-FYMQK5CX.mjs");
      _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 import("./request-XABCMU25.mjs");
      _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
    });
  }
}
__name(makeRequest, "makeRequest");
function getApiUrl() {
  if (_staticConfig?.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;
}
__name(getApiUrl, "getApiUrl");
function getAppUrl() {
  if (_staticConfig?.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(/\/+$/, "");
}
__name(getAppUrl, "getAppUrl");
function getTrackablePages() {
  return _staticConfig?.trackablePages ?? [];
}
__name(getTrackablePages, "getTrackablePages");
function runBootstrapper() {
  if (_staticConfig?.bootstrapper) {
    _staticConfig.bootstrapper();
  }
}
__name(runBootstrapper, "runBootstrapper");
function buildUrl(endpoint) {
  const apiUrl = getApiUrl();
  return endpoint.startsWith("http") ? endpoint : `${apiUrl}${endpoint}`;
}
__name(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: _staticConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language,
    paginationHandler: /* @__PURE__ */ __name(async (endpoint) => JsonApiGet({
      classKey: params.classKey,
      endpoint,
      companyId: params.companyId,
      language: params.language
    }), "paginationHandler")
  });
}
__name(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: _staticConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.responseType ?? params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
__name(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: _staticConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.responseType ?? params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
__name(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: _staticConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.responseType ?? params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
__name(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: _staticConfig?.additionalHeaders
  });
  return translateResponse({
    classKey: params.responseType ?? params.classKey,
    apiResponse,
    companyId: params.companyId,
    language: params.language
  });
}
__name(JsonApiDelete, "JsonApiDelete");

export {
  DataClassRegistry,
  JsonApiDataFactory,
  setBootstrapper,
  getBootstrapper,
  tryBootstrap,
  hasBootstrapper,
  resetBootstrapStore,
  translateData,
  translateResponse,
  configureJsonApi,
  getApiUrl,
  getAppUrl,
  getTrackablePages,
  JsonApiGet,
  JsonApiPost,
  JsonApiPut,
  JsonApiPatch,
  JsonApiDelete
};
//# sourceMappingURL=chunk-3VM3WAOV.mjs.map