"use strict";
/**
 * Accessibility Service
 * Handles accessibility checks using llmcli
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessibilityService = void 0;
const child_process_1 = require("child_process");
const logger_1 = require("@bluefly/llmcli/utils/logger");
class AccessibilityService {
    constructor() {
        // Use the installed llmcli from node_modules
        this.llmcliPath = require.resolve('@bluefly/llmcli/bin/llmcli.js');
    }
    static getInstance() {
        if (!AccessibilityService.instance) {
            AccessibilityService.instance = new AccessibilityService();
        }
        return AccessibilityService.instance;
    }
    async checkElement(elementHtml, options = {}) {
        return new Promise((resolve, reject) => {
            const args = [
                'accessibility',
                'check',
                '--output-format=json',
                '--severity=' + (options.severity || 'error'),
                options.useAI ? '--use-ai' : '--no-use-ai',
                options.scanUi ? '--scan-ui' : '--no-scan-ui',
                options.scanContent ? '--scan-content' : '--no-scan-content',
                '--conformance=' + (options.conformance || 'AA'),
            ];
            if (options.aiModel) {
                args.push('--ai-model=' + options.aiModel);
            }
            if (options.generateFixes) {
                args.push('--generate-fixes');
            }
            const process = (0, child_process_1.spawn)('node', [this.llmcliPath, ...args], {
                stdio: ['pipe', 'pipe', 'pipe']
            });
            let output = '';
            let error = '';
            // Write the HTML to stdin
            process.stdin.write(elementHtml);
            process.stdin.end();
            process.stdout.on('data', (data) => {
                output += data.toString();
            });
            process.stderr.on('data', (data) => {
                error += data.toString();
            });
            process.on('close', (code) => {
                if (code !== 0) {
                    logger_1.logger.error('Accessibility check failed:', error);
                    reject(new Error(`Accessibility check failed with code ${code}: ${error}`));
                    return;
                }
                try {
                    const results = JSON.parse(output);
                    resolve(results);
                }
                catch (e) {
                    logger_1.logger.error('Failed to parse accessibility results:', e);
                    reject(new Error('Failed to parse accessibility results'));
                }
            });
        });
    }
    async checkPage(url, options = {}) {
        // TODO: Implement page-level accessibility checking
        // This would involve fetching the page content and checking it
        throw new Error('Page-level accessibility checking not implemented yet');
    }
    async checkComponent(componentPath, options = {}) {
        // TODO: Implement component-level accessibility checking
        // This would involve analyzing React/Vue components
        throw new Error('Component-level accessibility checking not implemented yet');
    }
}
exports.AccessibilityService = AccessibilityService;
//# sourceMappingURL=accessibility-service.js.map