import { isRouteMatchCondition } from "../../../worker/service-worker/router.js";
export const requestMethod = makeRequestMethod();
function makeRequestMethod() {
    return {
        get: makeAddRequestMethodRouteAndHandler("get"),
        patch: makeAddRequestMethodRouteAndHandler("patch"),
        put: makeAddRequestMethodRouteAndHandler("put"),
        post: makeAddRequestMethodRouteAndHandler("post"),
        delete: makeAddRequestMethodRouteAndHandler("delete"),
        options: makeAddRequestMethodRouteAndHandler("options"),
        head: makeAddRequestMethodRouteAndHandler("head")
    };
}
function makeAddRequestMethodRouteAndHandler(requestMethod) {
    return function addRequestRouteAndHandler(pathnameOrInit, onRequest) {
        return addRequestMethodRouteAndHandler(requestMethod, pathnameOrInit, onRequest);
    };
}
function addRequestMethodRouteAndHandler(requestMethod, urlPattern, onRequest) {
    const tag = `${requestMethod}:${JSON.stringify(urlPattern)}`;
    const route = {
        condition: [
            {
                requestMethod
            },
            {
                urlPattern
            }
        ],
        source: {
            type: "fetch-event",
            tag
        }
    };
    self.addEventListener("install", event => {
        event.addRoutes(route);
    });
    self.addEventListener("fetch", event => {
        if (event.tag === tag) {
            event.waitUntil(intercept());
        }
        else if (!event.tag && isRouteMatchCondition(self.registration.durable, route, event.request)) {
            event.waitUntil(intercept());
        }
        async function intercept() {
            const response = await onRequest(event.request, event);
            if (response) {
                event.respondWith(response);
            }
        }
    });
}
//# sourceMappingURL=routes.example.js.map