"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.configureFrontendGenerator = exports.frontendGeneratorId = exports.toComponentImport = exports.all = exports.spreadsheet = exports.menus = exports.dialogs = exports.sections = exports.dataTable = exports.charts = exports.forms = exports.base = void 0;
const Path = __importStar(require("node:path"));
const Generator = __importStar(require("@postxl/generator"));
const generator_1 = require("@postxl/generator");
const tsconfig_generator_1 = require("./generators/tsconfig.generator");
const ROOT_ROUTE_FILENAME = '__root.tsx';
// CONTEXT REGISTRATION --------------------------------------------------------
exports.base = {
    components: [
        'avatar',
        'badge',
        'button',
        'calendar',
        'card',
        'checkbox',
        'combobox',
        'content-frame',
        'date-picker',
        'dialog',
        'dropdown-menu',
        'input',
        'label',
        'loader',
        'mark-value-renderer',
        'modal',
        'progress',
        'radio-group',
        'select',
        'sidebar',
        'skeleton',
        'slider',
        'spinner',
        'switch',
        'tabs',
        'textarea',
        'toast',
        'toggle',
        'toggle-group',
    ],
    hooks: ['useFontsLoaded.ts'],
    contextProviders: ['auth-context-provider.tsx', 'theme-context-provider.tsx'],
    packageJson: {
        // We set name and description with the context's slug in the register function as we do not have access to the schema here yet
        name: (0, generator_1.toPostXlPackageName)(`@postxl/template-frontend`),
        description: `Frontend for the template project`,
        type: 'module',
        dependencies: [
            { packageName: '@postxl/utils', version: '0.1.29' },
            { packageName: '@radix-ui/react-avatar', version: '1.1.10' },
            { packageName: '@radix-ui/react-checkbox', version: '1.3.3' },
            { packageName: '@radix-ui/react-dialog', version: '1.1.15' },
            { packageName: '@radix-ui/react-dropdown-menu', version: '2.1.16' },
            { packageName: '@radix-ui/react-icons', version: '1.3.2' },
            { packageName: '@radix-ui/react-label', version: '2.1.7' },
            { packageName: '@radix-ui/react-radio-group', version: '1.3.8' },
            { packageName: '@radix-ui/react-select', version: '2.2.6' },
            { packageName: '@radix-ui/react-slider', version: '1.3.6' },
            { packageName: '@radix-ui/react-slot', version: '1.2.3' },
            { packageName: '@radix-ui/react-switch', version: '1.2.6' },
            { packageName: '@radix-ui/react-tabs', version: '1.1.13' },
            { packageName: '@radix-ui/react-toggle', version: '1.1.10' },
            { packageName: '@radix-ui/react-toggle-group', version: '1.1.11' },
            { packageName: '@tailwindcss/vite', version: '4.1.16' },
            { packageName: '@tanstack/react-devtools', version: '0.7.9' },
            { packageName: '@tanstack/react-router', version: '1.131.34' },
            { packageName: '@tanstack/react-router-devtools', version: '1.131.34' },
            { packageName: 'class-variance-authority', version: '0.7.1' },
            { packageName: 'clsx', version: '2.1.1' },
            { packageName: 'date-fns', version: '4.1.0' },
            { packageName: 'keycloak-js', version: '26.2.1' },
            { packageName: 'lucide-react', version: '0.553.0' },
            { packageName: 'react', version: '19.1.1' },
            { packageName: 'react-day-picker', version: '9.11.1' },
            { packageName: 'react-dom', version: '19.1.1' },
            { packageName: 'sonner', version: '2.0.7' },
            { packageName: 'superjson', version: '1.13.1' },
            { packageName: 'tailwind-merge', version: '3.3.1' },
            { packageName: 'tailwindcss', version: '4.1.16' },
            { packageName: 'tw-animate-css', version: '1.4.0' },
            { packageName: 'use-local-storage-state', version: '19.5.0' },
            { packageName: 'zod', version: '3.25.76' },
        ],
        devDependencies: [
            { packageName: '@chromatic-com/storybook', version: '4.1.2' },
            { packageName: '@eslint/js', version: '9.39.1' },
            { packageName: 'eslint-plugin-react', version: '7.37.5' },
            { packageName: '@storybook/addon-docs', version: '10.0.0' },
            { packageName: '@storybook/addon-onboarding', version: '10.0.0' },
            { packageName: '@storybook/addon-vitest', version: '10.0.0' },
            { packageName: '@storybook/react-vite', version: '10.0.0' },
            { packageName: '@tanstack/devtools-ui', version: '0.4.4' },
            { packageName: '@tanstack/devtools-vite', version: '0.3.10' },
            { packageName: '@tanstack/router-plugin', version: '1.131.34' },
            { packageName: '@tanstack/router-cli', version: '1.131.34' },
            { packageName: '@types/geojson', version: '7946.0.16' },
            { packageName: '@types/react', version: '19.1.10' },
            { packageName: '@types/react-dom', version: '19.1.7' },
            { packageName: '@vitejs/plugin-basic-ssl', version: '2.1.0' },
            { packageName: '@vitejs/plugin-react', version: '5.1.0' },
            { packageName: '@vitest/browser', version: '3.2.4' },
            { packageName: '@vitest/coverage-v8', version: '3.2.4' },
            { packageName: 'eslint', version: '9.39.1' },
            { packageName: 'eslint-plugin-react-hooks', version: '7.0.1' },
            { packageName: 'eslint-plugin-react-refresh', version: '0.4.24' },
            { packageName: 'globals', version: '16.4.0' },
            { packageName: 'playwright', version: '1.56.1' },
            { packageName: 'storybook', version: '10.0.0' },
            { packageName: 'typescript', version: '~5.8.3' },
            { packageName: 'typescript-eslint', version: '8.46.3' },
            { packageName: 'vite', version: '7.1.12' },
            { packageName: 'vite-bundle-analyzer', version: '1.2.3' },
            { packageName: 'vite-plugin-svgr', version: '4.5.0' },
            { packageName: 'vite-tsconfig-paths', version: '5.1.4' },
            { packageName: 'vitest', version: '3.2.4' },
            { packageName: 'vitest-browser-react', version: '1.0.1' },
        ],
        scripts: [
            { name: 'build', command: 'pnpm test:types && vite build' },
            { name: 'build:analyze', command: 'vite build' },
            { name: 'debug', command: 'pnpm vite build --debug' },
            { name: 'dev', command: 'VITE_ENABLE_DEV_TOOLS=true vite' },
            { name: 'dev:noAuth', command: 'VITE_AUTH=false VITE_ENABLE_DEV_TOOLS=true vite' },
            {
                name: 'e2e:build',
                command: 'VITE_PUBLIC_API_URL=http://host.docker.internal:3001 VITE_PUBLIC_API_URL_2=http://host.docker.internal:3001 VITE_PUBLIC_BASE_URL=http://host.docker.internal:3000 NODE_V8_COVERAGE=.v8-coverage VITE_PUBLIC_MAPBOX_DISABLE_TRANSITIONS=true VITE_PUBLIC_USE_TEST_ASSETS=true VITE_AUTH=false vite build && vite preview --port 3000',
            },
            {
                name: 'e2e:ci',
                command: 'NODE_V8_COVERAGE=.v8-coverage VITE_PUBLIC_MAPBOX_DISABLE_TRANSITIONS=true VITE_PUBLIC_USE_TEST_ASSETS=true VITE_AUTH=false vite build && vite preview --port 3000',
            },
            { name: 'lint', command: 'eslint .' },
            { name: 'preview', command: 'vite preview' },
            { name: 'storybook:build', command: 'storybook build' },
            { name: 'storybook:start', command: 'storybook dev -p 6006' },
            { name: 'test:storybook', command: 'vitest run --project=storybook' },
            { name: 'test:types', command: 'tsc --noEmit -p tsconfig.json' },
            { name: 'test:types', command: 'tsc --noEmit' },
        ],
    },
    tsConfigReferences: new Map(),
};
/**
 * Can be used to augment the base config with our form input components.
 */
exports.forms = {
    components: [
        /*  'calendar',
        'checkbox',
        'data-picker',
        'dropdown-menu',
        'input',
        'label',*/
        'select',
        //'switch',
        //'textarea',
        // TODO add all ui components of PXL
        // 'input-color-picker',
        // 'input-common',
        // 'input-image',
        // 'input-mask',
        // 'input-number',
        // 'input-search',
        // 'input-table-select',
        // 'input-text',
        // 'input-tree-search',
    ],
    packageJson: {
        dependencies: [
            { packageName: '@floating-ui/react', version: '0.26.1' },
            { packageName: '@radix-ui/react-checkbox', version: '1.1.2' },
            { packageName: '@radix-ui/react-dropdown-menu', version: '2.1.16' },
            { packageName: '@radix-ui/react-label', version: '2.1.7' },
            { packageName: '@radix-ui/react-radio-group', version: '1.2.0' },
            { packageName: '@radix-ui/react-select', version: '2.2.6' },
            { packageName: '@radix-ui/react-switch', version: '1.2.6' },
            { packageName: 'date-fns', version: '4.1.0' },
            { packageName: 'luxon', version: '3.3.0' },
            { packageName: 'react-day-picker', version: '9.11.1' },
            { packageName: 'react-use', version: '17.4.0' },
            { packageName: 'react-hook-form', version: '7.62.0' },
        ],
    },
};
/**
 * TODO backport chats from portfolex (when ready)
 */
exports.charts = {
    components: ['chart'],
    packageJson: { dependencies: [{ packageName: 'recharts', version: '2.13.3' }] },
};
/**
 * Glide Data Grid exposed as data-table.
 * TODO new Data-Grid https://tablecn.com/data-grid
 */
exports.dataTable = {
    components: ['data-table', 'pagination', 'data-grid'],
    packageJson: {
        dependencies: [
            { packageName: '@glideapps/glide-data-grid', version: '6.0.3' },
            { packageName: '@visx/responsive', version: '3.10.2' },
            { packageName: '@tanstack/react-table', version: '8.21.3' },
            { packageName: '@tanstack/react-virtual', version: '3.13.12' },
            { packageName: 'lucide-react', version: '0.554.0' },
        ],
    },
};
exports.sections = {
    components: [
        'accordion',
        'breadcrumb',
        'carousel',
        'collapse',
        'pagination',
        'resizable',
        'separator',
        'scroll-area',
    ],
    packageJson: {
        dependencies: [
            { packageName: '@radix-ui/react-accordion', version: '1.2.12' },
            { packageName: '@radix-ui/react-collapsible', version: '1.1.1' },
            { packageName: '@radix-ui/react-separator', version: '1.1.7' },
            { packageName: '@radix-ui/react-scroll-area', version: '1.2.10' },
            { packageName: 'embla-carousel-react', version: '8.5.1' },
            { packageName: 'react-resizable-panels', version: '3.0.6' },
        ],
    },
};
/**
 *
 */
exports.dialogs = {
    components: [
        'alert',
        'alert-dialog',
        'card-hover',
        'command',
        'command-palette',
        'drawer',
        'popover',
        'sheet',
        'tooltip',
    ],
    packageJson: {
        dependencies: [
            { packageName: '@radix-ui/react-alert-dialog', version: '1.1.15' },
            { packageName: '@radix-ui/react-dialog', version: '1.1.15' },
            { packageName: '@radix-ui/react-hover-card', version: '1.1.15' },
            { packageName: '@radix-ui/react-popover', version: '1.1.15' },
            { packageName: '@radix-ui/react-tooltip', version: '1.2.8' },
            { packageName: 'cmdk', version: '1.1.1' },
            { packageName: 'vaul', version: '1.1.2' },
        ],
    },
};
/**
 *
 */
exports.menus = {
    components: ['context-menu', 'dropdown-menu', 'input', 'menubar', 'navigation-menu'],
    packageJson: {
        dependencies: [
            { packageName: '@radix-ui/react-context-menu', version: '2.2.16' },
            { packageName: '@radix-ui/react-dropdown-menu', version: '2.1.16' },
            { packageName: '@radix-ui/react-menubar', version: '1.1.16' },
            { packageName: '@radix-ui/react-navigation-menu', version: '1.2.14' },
        ],
    },
};
exports.spreadsheet = {
    components: ['spreadsheet'],
    packageJson: {
        dependencies: [],
    },
};
/**
 *
 */
const all = (config) => {
    return (config
        //
        .with(exports.charts)
        .with(exports.dataTable)
        .with(exports.sections)
        .with(exports.dialogs)
        .with(exports.menus)
        .with(exports.forms)
        .with(exports.spreadsheet));
};
exports.all = all;
// CODE GENERATION -------------------------------------------------------------
const toComponentImport = (name, exposing) => exposing.length === 0
    ? `import * as ${name
        .split('-')
        .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
        .join('')} from '@components/${name}'`
    : `import { ${exposing.join(', ')} } from '@components/ui/${name}/${name}'`;
exports.toComponentImport = toComponentImport;
exports.frontendGeneratorId = Generator.toGeneratorInterfaceId('frontend-core');
const configureFrontendGenerator = (into) => ({
    id: exports.frontendGeneratorId,
    implementsInterface: [exports.frontendGeneratorId],
    requires: [],
    register: (context) => {
        const frontend = into ? into(new generator_1.ConfigBuilder(exports.base)).done() : exports.base;
        frontend.packageJson.name = (0, generator_1.toPostXlPackageName)(`@postxl/${context.schema.slug}-frontend`);
        frontend.packageJson.description = `Frontend for the ${context.schema.slug} project`;
        return { ...context, frontend };
    },
    // TODO @Peter: context type <Context extends ContextResult>(context: Context): void => {
    afterRegistrations: (context) => {
        for (const backendModule of context.backend.modules) {
            context.frontend.tsConfigReferences.set(backendModule.name, {
                name: backendModule.name,
                filePath: Generator.toFilePath(`../../backend/libs/${backendModule.name}/src`),
                hasIndex: backendModule.hasIndex ?? false,
            });
        }
    },
    generate: async (context) => {
        const template = Path.join(__dirname, './template');
        const target = '/frontend';
        const copyToRelativeLocation = (path) => {
            const diskPath = Path.join(template, path);
            const targetPath = Path.join(target, path);
            return { diskPath, targetPath };
        };
        context.vfs.write(Path.join(target, 'package.json'), (0, generator_1.generatePackageJson)(context.frontend.packageJson));
        await context.vfs.loadFolder({ ...copyToRelativeLocation('./'), recursive: false });
        await context.vfs.loadFolder(copyToRelativeLocation('public'));
        await context.vfs.loadFolder(copyToRelativeLocation('.storybook'));
        await context.vfs.loadFolder({ ...copyToRelativeLocation('src'), recursive: false });
        await context.vfs.loadFolder(copyToRelativeLocation('src/pages'));
        await context.vfs.loadFolder({
            ...copyToRelativeLocation('src/routes'),
            filter: (filePath) => !filePath.endsWith(ROOT_ROUTE_FILENAME),
        });
        // Load __root.tsx with template substitution for schema.name
        const rootRouteContent = await (0, generator_1.generateFromTemplate)({
            file: Path.join(template, 'src/routes', ROOT_ROUTE_FILENAME),
            context: { schema: context.schema },
        });
        if (rootRouteContent.isErr()) {
            throw new Error(`Failed to generate ${ROOT_ROUTE_FILENAME}: ${rootRouteContent.unwrapErr().message}`);
        }
        context.vfs.write(Path.join(target, 'src/routes', ROOT_ROUTE_FILENAME), rootRouteContent.unwrap());
        await context.vfs.loadFolder(copyToRelativeLocation('src/stories'));
        // await context.vfs.loadFolder(copyToRelativeLocation('src/i18n'))
        await context.vfs.loadFolder(copyToRelativeLocation('src/lib'));
        await context.vfs.loadFolder(copyToRelativeLocation('src/styles'));
        for (const hook of new Set(context.frontend.hooks)) {
            await context.vfs.loadFile(copyToRelativeLocation(`src/hooks/${hook}`));
        }
        for (const component of new Set(context.frontend.components)) {
            await context.vfs.loadFolder(copyToRelativeLocation(`src/components/ui/${component}`));
        }
        for (const contextProvider of new Set(context.frontend.contextProviders)) {
            await context.vfs.loadFile(copyToRelativeLocation(`src/context-providers/${contextProvider}`));
        }
        context.vfs.write(Path.join(target, 'tsconfig.json'), (0, tsconfig_generator_1.generateTsConfig)(context));
        context.vfs.write(Path.join(target, '.env'), context.vfs.get(Path.join(target, '.env.example')) ?? '');
        return context;
    },
});
exports.configureFrontendGenerator = configureFrontendGenerator;
//# sourceMappingURL=frontend.generator.js.map