import { SemanticType } from '../Semantics.js';
/**
 * Type guard to check if a semantic is a SKU semantic.
 */
export const isSKUSemantic = (semantic) => {
    return semantic.id === SemanticType.SKU;
};
/**
 * Helper function to create a SKU semantic with configuration.
 */
export const createSKUSemantic = (config = {}) => {
    const mergedConfig = {
        ...DEFAULT_SKU_CONFIG,
        ...config,
    };
    // Merge metadata separately
    if (config.metadata) {
        mergedConfig.metadata = { ...config.metadata };
    }
    return {
        id: SemanticType.SKU,
        config: mergedConfig,
    };
};
/**
 * Default configuration for SKU semantic.
 * Optimized for common product catalog use cases.
 */
export const DEFAULT_SKU_CONFIG = {
    validationMode: 'strict',
    caseMode: 'uppercase',
    enforceUniqueness: true,
    autoGenerate: false,
    validateReservedWords: true,
    reservedValues: ['ADMIN', 'TEST', 'NULL', 'DEFAULT', 'UNDEFINED', 'SAMPLE', 'DEMO'],
};
/**
 * Predefined configurations for common use cases.
 */
export const SKU_PRESETS = {
    /**
     * Standard product SKU with strict validation and uppercase formatting.
     */
    PRODUCT_STANDARD: createSKUSemantic({
        validationMode: 'strict',
        caseMode: 'uppercase',
        prefix: 'PROD-',
        enforceUniqueness: true,
        validateReservedWords: true,
        reservedValues: ['ADMIN', 'TEST', 'NULL', 'DEFAULT'],
    }),
    /**
     * Simple SKU configuration for basic catalogs.
     */
    SIMPLE: createSKUSemantic({
        validationMode: 'lenient',
        caseMode: 'preserve',
        enforceUniqueness: true,
    }),
    /**
     * Auto-generating SKU from product name.
     */
    AUTO_GENERATE: createSKUSemantic({
        validationMode: 'strict',
        caseMode: 'uppercase',
        autoGenerate: true,
        autoGenerateSource: 'name',
        enforceUniqueness: true,
    }),
    /**
     * Flexible SKU for variable product types.
     */
    FLEXIBLE: createSKUSemantic({
        validationMode: 'lenient',
        caseMode: 'preserve',
        enforceUniqueness: true,
        validateReservedWords: false,
    }),
};
/**
 * Helper function to validate a SKU configuration.
 */
export const validateSKUConfig = (config) => {
    const errors = [];
    if (config.validationMode === 'custom' && !config.customPattern) {
        errors.push('customPattern is required when validationMode is custom');
    }
    if (config.customPattern) {
        try {
            new RegExp(config.customPattern);
        }
        catch {
            errors.push('customPattern must be a valid regular expression');
        }
    }
    if (config.prefix && config.prefix.length === 0) {
        errors.push('prefix cannot be empty string');
    }
    if (config.autoGenerateSource && !config.autoGenerate) {
        errors.push('autoGenerate must be true when autoGenerateSource is specified');
    }
    return errors;
};
/**
 * Helper function to validate SKU value against configuration.
 */
export const validateSKUValue = (value, config = DEFAULT_SKU_CONFIG) => {
    const errors = [];
    const mergedConfig = { ...DEFAULT_SKU_CONFIG, ...config };
    if (!value || typeof value !== 'string') {
        errors.push('SKU value must be a non-empty string');
        return errors;
    }
    if (mergedConfig.validateReservedWords && mergedConfig.reservedValues) {
        const normalizedValue = value.toUpperCase();
        if (mergedConfig.reservedValues.some((reserved) => reserved.toUpperCase() === normalizedValue)) {
            errors.push(`SKU cannot use reserved value: ${value}`);
        }
    }
    // Validation pattern checks
    if (mergedConfig.validationMode === 'strict') {
        if (!/^[A-Za-z0-9_-]+$/.test(value)) {
            errors.push('SKU can only contain alphanumeric characters, hyphens, and underscores');
        }
    }
    else if (mergedConfig.validationMode === 'lenient') {
        if (!/^[A-Za-z0-9_.-]+$/.test(value)) {
            errors.push('SKU contains invalid characters');
        }
    }
    else if (mergedConfig.validationMode === 'custom' && mergedConfig.customPattern) {
        try {
            const regex = new RegExp(mergedConfig.customPattern);
            if (!regex.test(value)) {
                errors.push('SKU does not match the required pattern');
            }
        }
        catch {
            errors.push('Invalid custom pattern configuration');
        }
    }
    return errors;
};
//# sourceMappingURL=SKU.js.map