/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { camelize, decamelize, ImportError, MaybePromise } from "@matter/general";
import { load } from "@matter/node/load";
const cache = {};
var loader;
((loader2) => {
  function behavior(name) {
    const exportName = `${camelize(name, true)}Behavior`;
    return doLoad(
      `behavior ${name}`,
      exportName,
      `../behaviors/${decamelize(name)}/${exportName}.js`
    );
  }
  loader2.behavior = behavior;
  function server(name) {
    const exportName = `${camelize(name, true)}Server`;
    return doLoad(
      `behavior ${name}`,
      exportName,
      `../behaviors/${decamelize(name)}/${exportName}.js`
    );
  }
  loader2.server = server;
  function device(name) {
    const exportName = `${camelize(name, true)}Device`;
    return doLoad(
      `device type ${name}`,
      exportName,
      `../devices/${decamelize(name)}.js`
    );
  }
  loader2.device = device;
  function endpoint(name) {
    const exportName = `${camelize(name, true)}Endpoint`;
    return doLoad(
      `endpoint type ${name}`,
      exportName,
      `../endpoints/${decamelize(name)}.js`
    );
  }
  loader2.endpoint = endpoint;
  loader2.registry = {};
})(loader || (loader = {}));
function doLoad(description, exportName, path) {
  const cachedResult = cache[path];
  if (cachedResult) {
    return cachedResult;
  }
  function extractExport(module) {
    if (exportName in module) {
      return module[exportName];
    }
    failure();
  }
  try {
    const module = load(path);
    if (MaybePromise.is(module)) {
      return module.then(extractExport, failure);
    }
    return extractExport(module);
  } catch (e) {
    failure();
  }
  function failure() {
    throw new ImportError(`No implementation available for ${description}`);
  }
}
export {
  loader
};
//# sourceMappingURL=loader.js.map
