import type { App, Component } from "vue";

const modules: Record<string, { default: Component }> = import.meta.glob("./*/*.vue", { eager: true });

/**
 * @example /avatar/index.vue => UiAvatar
 * @example /tabs/index.vue => UiTabs
 * @example /tabs/tab-pane.vue => UiTabPane
 */
function setupUiGlobalComponents(app: App) {
  for (const path in modules) {
    const parts = path.replace(/^\.\//, "").replace(/\.vue$/, "").split("/");
    const name = parts[parts.length - 1] === "index" ? parts[parts.length - 2] : parts[parts.length - 1];
    const componentName = `Ui${name.split("-").map(word => word.charAt(0).toUpperCase() + word.slice(1)).join("")}`;
    app.component(componentName, modules[path].default);
  }
}

export default {
  install: setupUiGlobalComponents,
};

// Global component types
declare module "vue" {
  export interface GlobalComponents {
    UiAvatar: typeof import("./avatar/index.vue")["default"];
    UiCollapse: typeof import("./collapse/index.vue")["default"];
    UiDatetime: typeof import("./datetime/index.vue")["default"];
    UiDivider: typeof import("./divider/index.vue")["default"];
    UiFileUpload: typeof import("./file-upload/index.vue")["default"];
    UiInput: typeof import("./input/index.vue")["default"];
    UiInputLabel: typeof import("./input-label/index.vue")["default"];
    UiResult: typeof import("./result/index.vue")["default"];
    UiTabPane: typeof import("./tabs/tab-pane.vue")["default"];
    UiTabs: typeof import("./tabs/index.vue")["default"];
    UiTag: typeof import("./tag/index.vue")["default"];
    UiTextareaLabel: typeof import("./textarea-label/index.vue")["default"];
    UiBackButton: typeof import("./back-button/index.vue")["default"];
  }
}
