"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminSessionService = void 0;
const common_1 = require("@nestjs/common");
const jwt = __importStar(require("jsonwebtoken"));
const ms_1 = __importDefault(require("ms"));
const admin_console_config_service_1 = require("./admin-console-config.service");
let AdminSessionService = class AdminSessionService {
    config;
    constructor(config) {
        this.config = config;
    }
    createSession(admin) {
        const secret = this.config.getSessionSecret();
        const duration = this.config.getSessionDuration();
        const expiresIn = typeof duration === 'number' ? duration : duration || '2h';
        return jwt.sign({
            sub: admin.id,
            email: admin.email,
        }, secret, {
            expiresIn,
        });
    }
    verifySession(token) {
        if (!token) {
            return null;
        }
        try {
            return jwt.verify(token, this.config.getSessionSecret());
        }
        catch (error) {
            console.warn('JWT verification failed:', error.message);
            return null;
        }
    }
    getCookieName() {
        return this.config.getCookieName();
    }
    getMaxAge() {
        const duration = this.config.getSessionDuration();
        if (typeof duration === 'number') {
            return duration * 1000;
        }
        if (typeof duration === 'string') {
            return (0, ms_1.default)(duration);
        }
        return (0, ms_1.default)('2h');
    }
    extractToken(request) {
        const cookieName = this.getCookieName();
        const cookieHeader = request.headers?.cookie;
        if (request.cookies && request.cookies[cookieName]) {
            return request.cookies[cookieName];
        }
        if (!cookieHeader) {
            return undefined;
        }
        const cookies = this.parseCookieHeader(cookieHeader);
        return cookies[cookieName];
    }
    async invalidateSessionForAdmin(adminId) {
        return Promise.resolve();
    }
    parseCookieHeader(header) {
        return header.split(';').reduce((acc, part) => {
            const [key, ...rest] = part.split('=');
            if (!key) {
                return acc;
            }
            try {
                acc[key.trim()] = decodeURIComponent(rest.join('=').trim());
            }
            catch (error) {
                acc[key.trim()] = rest.join('=').trim();
            }
            return acc;
        }, {});
    }
};
exports.AdminSessionService = AdminSessionService;
exports.AdminSessionService = AdminSessionService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [admin_console_config_service_1.AdminConsoleConfigService])
], AdminSessionService);
//# sourceMappingURL=admin-session.service.js.map