import { Logger } from '../../../../../src/index.js';
// Import individual validators
import { withDatacenterValidation } from './DatacenterValidator.js';
import { withFeaturesValidation } from './FeaturesValidator.js';
import { withInfrastructureValidation } from './InfrastructureValidator.js';
import { withStrictPayloadKeys } from './PayloadSanitizer.js';
import { withProcessorPathValidation } from './ProcessorPathSanitizer.js';
import { withQueueNameValidation } from './QueueNameSanitizer.js';
import { withVersionValidation } from './VersionSanitizer.js';
import { withWorkerNameValidation } from './WorkerNameSanitizer.js';
/**
 * Composite middleware for worker edit validation
 * Maps processorSpec to processor for validation and validates all editable fields
 */
export const withEditWorkerValidation = (handler) => {
    return async (req, res) => {
        try {
            const data = req.data();
            const currentBody = req.getBody();
            // Map processorSpec/processorSpec to processor for validation if provided
            let mappedBody = { ...currentBody };
            if (data['processorSpec'] && !data['processor']) {
                mappedBody = {
                    ...mappedBody,
                    processor: data['processorSpec'],
                };
            }
            // Update the request body with mapped fields
            req.setBody(mappedBody);
            // Apply validation with mapped fields, skipping options validation for editing
            return withStrictPayloadKeys([
                'name',
                'queueName',
                'processor', // Validated field (mapped from processorSpec)
                'processorSpec',
                'version',
                'options', // Skip strict validation for editing
                'infrastructure',
                'features',
                'datacenter',
                'concurrency', // Original field
                'region',
                'autoStart',
                'activeStatus',
                'status',
            ], withProcessorPathValidation(withWorkerNameValidation(withQueueNameValidation(withVersionValidation(withInfrastructureValidation(withFeaturesValidation(withDatacenterValidation(handler))))))))(req, res);
        }
        catch (error) {
            Logger.error('Edit worker validation failed', error);
            return res.setStatus(500).json({
                error: 'Internal validation error',
                code: 'VALIDATION_ERROR',
            });
        }
    };
};
