import React from "react";
import { TemplateService } from "./services/template.service";
import {
  canOverrideComponent,
  canOverrideMetadata,
  getProtectionLevel
} from "../config/protected-paths";
function getTemplateOrDefault(appPath, defaultComponent) {
  console.log(`[getTemplateOrDefault] Called for: ${appPath}`);
  if (!canOverrideComponent(appPath)) {
    if (process.env.NODE_ENV === "development") {
      const level = getProtectionLevel(appPath);
      console.log(`\u{1F6E1}\uFE0F  Component override blocked for ${appPath} (${level})`);
    }
    return defaultComponent;
  }
  if (TemplateService.hasOverride(appPath)) {
    const template = TemplateService.getComponent(appPath);
    if (template) {
      if (process.env.NODE_ENV === "development") {
        console.log(`\u{1F3A8} Component override applied for ${appPath}`);
      }
      return template;
    }
  }
  return defaultComponent;
}
function getMetadataOrDefault(appPath, defaultMetadata) {
  var _a;
  if (!canOverrideMetadata(appPath)) {
    if (process.env.NODE_ENV === "development") {
      const level = getProtectionLevel(appPath);
      console.log(`\u{1F6E1}\uFE0F  Metadata override blocked for ${appPath} (${level})`);
    }
    return defaultMetadata;
  }
  if (TemplateService.hasOverride(appPath)) {
    const entry = TemplateService.getEntry(appPath);
    if ((_a = entry == null ? void 0 : entry.template) == null ? void 0 : _a.metadata) {
      if (process.env.NODE_ENV === "development") {
        console.log(`\u{1F3A8} Metadata override applied for ${appPath}`);
      }
      return entry.template.metadata;
    }
  }
  if (process.env.NODE_ENV === "development") {
    console.log(`\u{1F4DD} Using default metadata for ${appPath}`);
  }
  return defaultMetadata;
}
function useTemplateOverride(pathname) {
  const appPath = `app${pathname === "/" ? "/page.tsx" : `${pathname}/page.tsx`}`;
  const layoutPath = `app${pathname === "/" ? "/layout.tsx" : `${pathname}/layout.tsx`}`;
  const pageOverride = TemplateService.getEntry(appPath);
  const layoutOverride = TemplateService.getEntry(layoutPath);
  return {
    hasPageOverride: !!pageOverride,
    hasLayoutOverride: !!layoutOverride,
    pageTemplate: pageOverride == null ? void 0 : pageOverride.template,
    layoutTemplate: layoutOverride == null ? void 0 : layoutOverride.template,
    getPageComponent: () => TemplateService.getComponent(appPath),
    getLayoutComponent: () => TemplateService.getComponent(layoutPath)
  };
}
function getTemplateWithInfo(appPath) {
  return TemplateService.resolve(appPath);
}
function debugTemplateOverrides() {
  if (process.env.NODE_ENV === "development") {
    console.group("\u{1F3A8} Template Override System");
    const overrides = Object.entries({
      "app/dashboard/layout.tsx": TemplateService.getEntry("app/dashboard/layout.tsx"),
      "app/dashboard/page.tsx": TemplateService.getEntry("app/dashboard/page.tsx")
    });
    overrides.forEach(([path, entry]) => {
      if (entry) {
        console.log(`\u2705 ${path} \u2192 ${entry.template.themeName} (${entry.template.templateType})`);
      } else {
        console.log(`\u26AA ${path} \u2192 Using default`);
      }
    });
    console.groupEnd();
  }
}
function withTemplateOverride(Component, appPath) {
  const WrappedComponent = (props) => {
    if (!canOverrideComponent(appPath)) {
      if (process.env.NODE_ENV === "development") {
        const level = getProtectionLevel(appPath);
        console.log(`\u{1F6E1}\uFE0F  HOC component override blocked for ${appPath} (${level})`);
      }
      return React.createElement(Component, props);
    }
    if (TemplateService.hasOverride(appPath)) {
      const OverrideComponent = TemplateService.getComponent(appPath);
      if (OverrideComponent) {
        if (process.env.NODE_ENV === "development") {
          console.log(`\u{1F3A8} HOC component override applied for ${appPath}`);
        }
        return React.createElement(OverrideComponent, props);
      }
    }
    return React.createElement(Component, props);
  };
  WrappedComponent.displayName = `withTemplateOverride(${Component.displayName || Component.name})`;
  return WrappedComponent;
}
function withTemplateOverrideFor(appPath) {
  return function(Component) {
    return withTemplateOverride(Component, appPath);
  };
}
export {
  debugTemplateOverrides,
  getMetadataOrDefault,
  getTemplateOrDefault,
  getTemplateWithInfo,
  useTemplateOverride,
  withTemplateOverride,
  withTemplateOverrideFor
};
