"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCookieHeader = void 0;
function createCookieHeader({ url, cookies = [] }) {
    const resourceUrl = new URL(url);
    return cookies.reduce((header, cookie) => {
        var _a;
        const domainMatch = ((_a = cookie.domain) === null || _a === void 0 ? void 0 : _a.startsWith('.'))
            ? resourceUrl.hostname.includes(cookie.domain.substring(1))
            : resourceUrl.hostname === cookie.domain;
        if (!domainMatch)
            return header;
        const pathMatch = !!cookie.path && resourceUrl.pathname.startsWith(cookie.path);
        if (!pathMatch)
            return header;
        const secureProtocol = resourceUrl.protocol === 'https:';
        if (cookie.secure && !secureProtocol)
            return header;
        const expired = !!cookie.expiry && cookie.expiry >= 0 && Date.now() > cookie.expiry * 1000;
        if (expired)
            return header;
        return header + `${cookie.name}=${cookie.value};`;
    }, '');
}
exports.createCookieHeader = createCookieHeader;
