"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAdminSidebar = generateAdminSidebar;
const Generator = __importStar(require("@postxl/generator"));
const utils_1 = require("../utils");
/**
 * Generator function that generates the shared Admin sidebar component
 */
function generateAdminSidebar({ context }) {
    const imports = Generator.ImportGenerator
        //
        .from(Generator.toBackendModuleLocation('@ui/admin/AdminSidebar'))
        .addImport({
        from: Generator.toPackageName('react'),
        items: [Generator.toFunctionName('useState')],
    })
        .addImport({
        from: Generator.toPackageName('@radix-ui/react-icons'),
        items: [
            Generator.toFunctionName('ChevronRightIcon'),
            Generator.toFunctionName('ChevronDownIcon'),
            Generator.toFunctionName('ChevronLeftIcon'),
        ],
    });
    const { schemas: sortedSchemas, modelsBySchema } = (0, utils_1.groupAndSortModelsBySchema)(context);
    // Generate the schema sections
    const schemaSections = sortedSchemas
        .map((schema) => {
        const models = modelsBySchema.get(schema);
        const modelLinks = models
            .map((model) => `
          <li key="${model.name}">
            <Link
              to="${model.route}"
              className="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-800 rounded transition-colors"
            >
              ${model.userFriendlyName}
            </Link>
          </li>`)
            .join('');
        return `
    <div key="${schema}" className="mb-4">
      <button
        onClick={() => toggleSchema('${schema}')}
        className="flex items-center justify-between w-full px-4 py-2 font-semibold text-left hover:bg-gray-100 dark:hover:bg-gray-800 rounded transition-colors"
      >
        <span>${schema}</span>
        {expandedSchemas['${schema}'] ? (
          <ChevronDownIcon className="w-4 h-4" />
        ) : (
          <ChevronRightIcon className="w-4 h-4" />
        )}
      </button>
      {expandedSchemas['${schema}'] && (
        <ul className="mt-2 ml-2 space-y-1">
          ${modelLinks}
        </ul>
      )}
    </div>`;
    })
        .join('');
    return `
import { Link } from '@tanstack/react-router'
${imports.generate()}

export function AdminSidebar() {
  const [isOpen, setIsOpen] = useState(false)
  const [isCollapsed, setIsCollapsed] = useState(false)
  const [expandedSchemas, setExpandedSchemas] = useState<Record<string, boolean>>({
    ${sortedSchemas.map((schema) => `'${schema}': true`).join(',\n    ')},
    'Advanced': true
  })

  const toggleSchema = (schema: string) => {
    setExpandedSchemas((prev) => ({
      ...prev,
      [schema]: !prev[schema],
    }))
  }

  return (
    <>
      {/* Mobile toggle button */}
      <button
        onClick={() => setIsOpen(!isOpen)}
        className="fixed top-4 left-4 z-50 md:hidden p-2 bg-white dark:bg-gray-900 rounded shadow-lg border border-gray-200 dark:border-gray-700"
        aria-label="Toggle admin menu"
      >
        {isOpen ? <ChevronDownIcon className="w-6 h-6" /> : <ChevronRightIcon className="w-6 h-6" />}
      </button>

      {/* Sidebar */}
      <aside
        className={\`
          fixed md:sticky top-0 left-0 h-screen bg-white dark:bg-gray-900 border-r border-gray-200 dark:border-gray-700
          overflow-y-auto transition-all duration-300 z-40
          \${isOpen ? 'translate-x-0' : '-translate-x-full md:translate-x-0'}
          \${isCollapsed ? 'w-16 min-w-16' : 'w-64 min-w-64'}
        \`}
      >
        <div className="p-4">
          <div className="flex items-center justify-between mb-4">
            {!isCollapsed && (
              <Link to="/admin" className="text-lg font-bold hover:text-blue-600 dark:hover:text-blue-400 transition-colors">
                Admin
              </Link>
            )}
            <button
              onClick={() => setIsCollapsed(!isCollapsed)}
              className="hidden md:block p-1 hover:bg-gray-100 dark:hover:bg-gray-800 rounded transition-colors ml-auto"
              aria-label={isCollapsed ? 'Expand sidebar' : 'Collapse sidebar'}
            >
              {isCollapsed ? <ChevronRightIcon className="w-5 h-5" /> : <ChevronLeftIcon className="w-5 h-5" />}
            </button>
          </div>
          {!isCollapsed && (
            <nav>
              ${schemaSections}
              {import.meta.env.NEXT_PUBLIC_ALLOW_ADMIN === 'true' && (
                <div key="Advanced" className="mb-4">
                  <button
                    onClick={() => toggleSchema('Advanced')}
                    className="flex items-center justify-between w-full px-4 py-2 font-semibold text-left hover:bg-gray-100 dark:hover:bg-gray-800 rounded transition-colors"
                  >
                    <span>Advanced</span>
                    {expandedSchemas['Advanced'] ? (
                      <ChevronDownIcon className="w-4 h-4" />
                    ) : (
                      <ChevronRightIcon className="w-4 h-4" />
                    )}
                  </button>
                  {expandedSchemas['Advanced'] && (
                    <ul className="mt-2 ml-2 space-y-1">
                      <li key="DataManagement">
                        <Link
                          to="/admin/data-management"
                          className="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-800 rounded transition-colors"
                        >
                          Data Management
                        </Link>
                      </li>
                    </ul>
                  )}
                </div>
              )}
            </nav>
          )}
        </div>
      </aside>

      {/* Overlay for mobile */}
      {isOpen && (
        <div
          className="fixed inset-0 bg-black/20 z-30 md:hidden"
          onClick={() => setIsOpen(false)}
        />
      )}
    </>
  )
}
`;
}
//# sourceMappingURL=admin-sidebar.generator.js.map