const FrameworkElementBase = HTMLElement;

function definePrimitiveTag(tag: string) {
  const registry = globalThis.customElements;
  if (!registry) return undefined;

  const existing = registry.get(tag);
  if (existing) return existing;

  const ElementBase = globalThis.HTMLElement ?? FrameworkElementBase;
  const PrimitiveTag = class extends ElementBase {};
  registry.define(tag, PrimitiveTag);
  return PrimitiveTag;
}

class MxInterface extends FrameworkElementBase {
  static define() {
    return definePrimitiveTag('mx-interface');
  }
}

class MxContract extends FrameworkElementBase {
  static define() {
    return definePrimitiveTag('mx-contract');
  }
}

class MxProxy extends FrameworkElementBase {
  static define() {
    return definePrimitiveTag('mx-proxy');
  }
}

class MxService extends FrameworkElementBase {
  static define() {
    return definePrimitiveTag('mx-service');
  }
}

class MxMiddlewares extends FrameworkElementBase {
  static define() {
    return definePrimitiveTag('mx-middlewares');
  }
}

export function defineComponents() {
  MxInterface.define();
  MxContract.define();
  MxProxy.define();
  MxService.define();
  MxMiddlewares.define();
}

export { MxInterface, MxContract, MxProxy, MxService, MxMiddlewares };
export { MxTopicTag } from './components/MxTopicTag';
