import crypto from 'crypto';
import { extractTranslations } from '../../translations/extractTranslations.js';
const labels = extractTranslations([
    'authentication:enableAPIKey',
    'authentication:apiKey'
]);
const encryptKey = ({ req, value })=>value ? req.payload.encrypt(value) : null;
const decryptKey = ({ req, value })=>value ? req.payload.decrypt(value) : undefined;
export default [
    {
        name: 'enableAPIKey',
        type: 'checkbox',
        admin: {
            components: {
                Field: ()=>null
            }
        },
        defaultValue: false,
        label: labels['authentication:enableAPIKey']
    },
    {
        name: 'apiKey',
        type: 'text',
        admin: {
            components: {
                Field: ()=>null
            }
        },
        hooks: {
            afterRead: [
                decryptKey
            ],
            beforeChange: [
                encryptKey
            ]
        },
        label: labels['authentication:apiKey']
    },
    {
        name: 'apiKeyIndex',
        type: 'text',
        admin: {
            disabled: true
        },
        hidden: true,
        hooks: {
            beforeValidate: [
                async ({ data, req, value })=>{
                    if (data.apiKey) {
                        return crypto.createHmac('sha1', req.payload.secret).update(data.apiKey).digest('hex');
                    }
                    if (data.enableAPIKey === false) {
                        return null;
                    }
                    return value;
                }
            ]
        }
    }
];

//# sourceMappingURL=apiKey.js.map