/**
 * commands/design-scan.mjs — `apt-tools design-scan <project-dir>` (C27).
 *
 * Walks the project for UI source files, extracts design tokens (colors,
 * spacing, typography, radii, shadows) via regex, and writes the result to
 * `.aperant/design/tokens.json` through `atomicWriteJson`.
 *
 * Usage:
 *
 *   apt-tools design-scan <project-dir> [--out <path>] [--root <subdir>]
 *
 * `--out` overrides the default output location (useful for tests). `--root`
 * limits the walker to a subdirectory of the project (e.g. `packages/ui`).
 *
 * Output envelope (stdout):
 *
 *   {
 *     status: 'ok',
 *     command: 'design-scan',
 *     written: '<abs path to tokens.json>',
 *     summary: { files_scanned, files_skipped, colors, spacing, typography,
 *                radii, shadows }  // counts per slice
 *   }
 *
 * The command intentionally skips schema validation on the output — if the
 * scanner produces an invalid shape that's a bug in scan.mjs, not a user
 * error. Tests cover the schema round-trip.
 */
import { join, resolve as resolvePath } from 'node:path';
import { scanDesignTokens } from '../design/scan.mjs';
import { parseFlags } from '../util/args.mjs';
import { atomicWriteJson } from '../util/io.mjs';
import { err, ok } from '../util/result.mjs';
export function cmdDesignScan(projectDir, extraArgs) {
    if (!projectDir) {
        return err('Usage: apt-tools design-scan <project-dir> [--out <path>] [--root <subdir>]');
    }
    const flags = parseFlags(extraArgs ?? []);
    const outFlag = flags.get('out');
    const rootFlag = flags.get('root');
    const absProject = resolvePath(projectDir);
    // Default output: <project>/.aperant/design/tokens.json. Explicit --out
    // wins (used by tests + ad-hoc scans that don't want to touch .aperant/).
    const outPath = typeof outFlag === 'string'
        ? resolvePath(outFlag)
        : join(absProject, '.aperant', 'design', 'tokens.json');
    const tokens = scanDesignTokens(absProject, {
        root: typeof rootFlag === 'string' ? rootFlag : undefined,
    });
    // Attach the timestamp here, not in scan.mjs, so scan.mjs stays pure
    // (important for snapshot tests that compare deterministic outputs).
    const payload = {
        ...tokens,
        generated_at: new Date().toISOString(),
    };
    // Re-key so the JSON output is in a human-readable field order:
    // version, generated_at, project_root, scan, then token groups.
    const ordered = {
        version: payload.version,
        generated_at: payload.generated_at,
        project_root: payload.project_root,
        scan: payload.scan,
        colors: payload.colors,
        spacing: payload.spacing,
        typography: payload.typography,
        radii: payload.radii,
        shadows: payload.shadows,
    };
    atomicWriteJson(outPath, ordered);
    return ok({
        status: 'ok',
        command: 'design-scan',
        written: outPath,
        summary: {
            files_scanned: ordered.scan.files_scanned,
            files_skipped: ordered.scan.files_skipped,
            colors: {
                hex: ordered.colors.hex.length,
                rgb: ordered.colors.rgb.length,
                hsl: ordered.colors.hsl.length,
                tailwind: ordered.colors.tailwind.length,
                css_vars: ordered.colors.css_vars.length,
            },
            spacing: {
                raw: ordered.spacing.raw.length,
                tailwind_scale: ordered.spacing.tailwind_scale.length,
            },
            typography: {
                font_families: ordered.typography.font_families.length,
                font_sizes_raw: ordered.typography.font_sizes_raw.length,
                tailwind_sizes: ordered.typography.tailwind_sizes.length,
                line_heights: ordered.typography.line_heights.length,
            },
            radii: {
                raw: ordered.radii.raw.length,
                tailwind: ordered.radii.tailwind.length,
            },
            shadows: {
                raw: ordered.shadows.raw.length,
                tailwind: ordered.shadows.tailwind.length,
            },
        },
    });
}
//# sourceMappingURL=design-scan.mjs.map