// Minimal cors shim for CF Workers
// Uses Object.assign pattern so both ESM and CJS interop work:
// - ESM: import cors from 'cors' -> cors is the function
// - CJS: const cors = require('cors') -> cors is the function (via __toCommonJS default)
function cors(_opts?: any) {
  return (_req: any, res: any, next: any) => {
    res?.set?.('Access-Control-Allow-Origin', '*');
    res?.set?.('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,PATCH,OPTIONS');
    res?.set?.('Access-Control-Allow-Headers', 'Content-Type,Authorization,x-user-did,x-csrf-token');
    if (_req?.method === 'OPTIONS') {
      res?.status?.(204)?.end?.();
      return;
    }
    next();
  };
}

// @ts-ignore - module.exports for CJS compat
module.exports = cors;
// @ts-ignore
module.exports.default = cors;
