var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
    var useValue = arguments.length > 2;
    for (var i = 0; i < initializers.length; i++) {
        value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
    }
    return useValue ? value : void 0;
};
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
    function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
    var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
    var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
    var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
    var _, done = false;
    for (var i = decorators.length - 1; i >= 0; i--) {
        var context = {};
        for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
        for (var p in contextIn.access) context.access[p] = contextIn.access[p];
        context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
        var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
        if (kind === "accessor") {
            if (result === void 0) continue;
            if (result === null || typeof result !== "object") throw new TypeError("Object expected");
            if (_ = accept(result.get)) descriptor.get = _;
            if (_ = accept(result.set)) descriptor.set = _;
            if (_ = accept(result.init)) initializers.unshift(_);
        }
        else if (_ = accept(result)) {
            if (kind === "field") initializers.unshift(_);
            else descriptor[key] = _;
        }
    }
    if (target) Object.defineProperty(target, contextIn.name, descriptor);
    done = true;
};
import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
/**
 * Built-in routes controller for TypedServer
 * Handles robots.txt, manifest.json, sitemap, feed, appversion
 */
let BuiltInRoutesController = (() => {
    let _classDecorators = [plugins.smartserve.Route('')];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _instanceExtraInitializers = [];
    let _getRobots_decorators;
    let _getManifest_decorators;
    let _getSitemap_decorators;
    let _getSitemapNews_decorators;
    let _getFeed_decorators;
    let _getAppVersion_decorators;
    let _getSwDash_decorators;
    let _getSwDashMetrics_decorators;
    let _getSwDashResources_decorators;
    let _getSwDashEvents_decorators;
    let _getSwDashEventsCount_decorators;
    let _getSwDashCumulativeMetrics_decorators;
    let _getSwDashSpeedtest_decorators;
    let _getSwDashBundle_decorators;
    let _getServiceWorkerBundle_decorators;
    var BuiltInRoutesController = class {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
            _getRobots_decorators = [plugins.smartserve.Get('/robots.txt')];
            _getManifest_decorators = [plugins.smartserve.Get('/manifest.json')];
            _getSitemap_decorators = [plugins.smartserve.Get('/sitemap')];
            _getSitemapNews_decorators = [plugins.smartserve.Get('/sitemap-news')];
            _getFeed_decorators = [plugins.smartserve.Get('/feed')];
            _getAppVersion_decorators = [plugins.smartserve.Get('/appversion')];
            _getSwDash_decorators = [plugins.smartserve.Get('/sw-dash')];
            _getSwDashMetrics_decorators = [plugins.smartserve.Get('/sw-dash/metrics')];
            _getSwDashResources_decorators = [plugins.smartserve.Get('/sw-dash/resources')];
            _getSwDashEvents_decorators = [plugins.smartserve.Get('/sw-dash/events')];
            _getSwDashEventsCount_decorators = [plugins.smartserve.Get('/sw-dash/events/count')];
            _getSwDashCumulativeMetrics_decorators = [plugins.smartserve.Get('/sw-dash/cumulative-metrics')];
            _getSwDashSpeedtest_decorators = [plugins.smartserve.Get('/sw-dash/speedtest')];
            _getSwDashBundle_decorators = [plugins.smartserve.Get('/sw-dash/bundle.js')];
            _getServiceWorkerBundle_decorators = [plugins.smartserve.Get('/serviceworker.bundle.js')];
            __esDecorate(this, null, _getRobots_decorators, { kind: "method", name: "getRobots", static: false, private: false, access: { has: obj => "getRobots" in obj, get: obj => obj.getRobots }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getManifest_decorators, { kind: "method", name: "getManifest", static: false, private: false, access: { has: obj => "getManifest" in obj, get: obj => obj.getManifest }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSitemap_decorators, { kind: "method", name: "getSitemap", static: false, private: false, access: { has: obj => "getSitemap" in obj, get: obj => obj.getSitemap }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSitemapNews_decorators, { kind: "method", name: "getSitemapNews", static: false, private: false, access: { has: obj => "getSitemapNews" in obj, get: obj => obj.getSitemapNews }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getFeed_decorators, { kind: "method", name: "getFeed", static: false, private: false, access: { has: obj => "getFeed" in obj, get: obj => obj.getFeed }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getAppVersion_decorators, { kind: "method", name: "getAppVersion", static: false, private: false, access: { has: obj => "getAppVersion" in obj, get: obj => obj.getAppVersion }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSwDash_decorators, { kind: "method", name: "getSwDash", static: false, private: false, access: { has: obj => "getSwDash" in obj, get: obj => obj.getSwDash }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSwDashMetrics_decorators, { kind: "method", name: "getSwDashMetrics", static: false, private: false, access: { has: obj => "getSwDashMetrics" in obj, get: obj => obj.getSwDashMetrics }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSwDashResources_decorators, { kind: "method", name: "getSwDashResources", static: false, private: false, access: { has: obj => "getSwDashResources" in obj, get: obj => obj.getSwDashResources }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSwDashEvents_decorators, { kind: "method", name: "getSwDashEvents", static: false, private: false, access: { has: obj => "getSwDashEvents" in obj, get: obj => obj.getSwDashEvents }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSwDashEventsCount_decorators, { kind: "method", name: "getSwDashEventsCount", static: false, private: false, access: { has: obj => "getSwDashEventsCount" in obj, get: obj => obj.getSwDashEventsCount }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSwDashCumulativeMetrics_decorators, { kind: "method", name: "getSwDashCumulativeMetrics", static: false, private: false, access: { has: obj => "getSwDashCumulativeMetrics" in obj, get: obj => obj.getSwDashCumulativeMetrics }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSwDashSpeedtest_decorators, { kind: "method", name: "getSwDashSpeedtest", static: false, private: false, access: { has: obj => "getSwDashSpeedtest" in obj, get: obj => obj.getSwDashSpeedtest }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getSwDashBundle_decorators, { kind: "method", name: "getSwDashBundle", static: false, private: false, access: { has: obj => "getSwDashBundle" in obj, get: obj => obj.getSwDashBundle }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _getServiceWorkerBundle_decorators, { kind: "method", name: "getServiceWorkerBundle", static: false, private: false, access: { has: obj => "getServiceWorkerBundle" in obj, get: obj => obj.getServiceWorkerBundle }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            BuiltInRoutesController = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
            __runInitializers(_classThis, _classExtraInitializers);
        }
        options = __runInitializers(this, _instanceExtraInitializers);
        constructor(options) {
            this.options = options;
        }
        async getRobots(ctx) {
            if (!this.options.robots || !this.options.domain) {
                throw new plugins.smartserve.RouteNotFoundError(ctx.path, ctx.method);
            }
            const robotsContent = [
                'User-agent: *',
                'Allow: /',
                `Sitemap: https://${this.options.domain}/sitemap`,
            ];
            if (this.options.blockWaybackMachine) {
                robotsContent.push('', 'User-agent: ia_archiver', 'Disallow: /');
            }
            return new Response(robotsContent.join('\n'), {
                status: 200,
                headers: { 'Content-Type': 'text/plain' },
            });
        }
        async getManifest(ctx) {
            if (!this.options.manifest) {
                throw new plugins.smartserve.RouteNotFoundError(ctx.path, ctx.method);
            }
            return new Response(this.options.manifest.jsonString(), {
                status: 200,
                headers: { 'Content-Type': 'application/json' },
            });
        }
        async getSitemap(ctx) {
            if (!this.options.sitemap || !this.options.domain) {
                throw new plugins.smartserve.RouteNotFoundError(ctx.path, ctx.method);
            }
            const urls = this.options.getSitemapUrls();
            const sitemapXml = plugins.smartsitemap.SmartSitemap.create().addUrls(urls).toXml();
            return new Response(sitemapXml, {
                status: 200,
                headers: { 'Content-Type': 'application/xml' },
            });
        }
        async getSitemapNews(ctx) {
            if (!this.options.sitemap || !this.options.domain || !this.options.articleGetterFunction) {
                throw new plugins.smartserve.RouteNotFoundError(ctx.path, ctx.method);
            }
            const articles = await this.options.articleGetterFunction();
            const sitemapNewsXml = plugins.smartsitemap.SmartSitemap.fromArticles(articles, { publicationName: this.options.domain || 'News' }).toXml();
            return new Response(sitemapNewsXml, {
                status: 200,
                headers: { 'Content-Type': 'application/xml' },
            });
        }
        async getFeed(ctx) {
            if (!this.options.feed || !this.options.feedMetadata) {
                throw new plugins.smartserve.RouteNotFoundError(ctx.path, ctx.method);
            }
            const smartfeed = new plugins.smartfeed.Smartfeed();
            const articles = this.options.articleGetterFunction
                ? await this.options.articleGetterFunction()
                : [];
            const feedXml = await smartfeed.createFeedFromArticleArray(this.options.feedMetadata, articles);
            return new Response(feedXml, {
                status: 200,
                headers: { 'Content-Type': 'application/atom+xml' },
            });
        }
        async getAppVersion(ctx) {
            if (!this.options.appVersion) {
                throw new plugins.smartserve.RouteNotFoundError(ctx.path, ctx.method);
            }
            return new Response(this.options.appVersion, {
                status: 200,
                headers: { 'Content-Type': 'text/plain' },
            });
        }
        async getSwDash(ctx) {
            // Import shared HTML from interfaces
            const { SW_DASH_HTML } = await import('../../dist_ts_interfaces/serviceworker.js');
            return new Response(SW_DASH_HTML, {
                status: 200,
                headers: { 'Content-Type': 'text/html' },
            });
        }
        // SW-dash data routes - return empty/unavailable when SW isn't active
        async getSwDashMetrics() {
            return new Response(JSON.stringify({ error: 'Service worker not active', data: null }), {
                status: 503,
                headers: { 'Content-Type': 'application/json' },
            });
        }
        async getSwDashResources() {
            return new Response(JSON.stringify({ error: 'Service worker not active', resources: [], domains: [], contentTypes: [], resourceCount: 0 }), {
                status: 503,
                headers: { 'Content-Type': 'application/json' },
            });
        }
        async getSwDashEvents() {
            return new Response(JSON.stringify({ error: 'Service worker not active', events: [], total: 0 }), {
                status: 503,
                headers: { 'Content-Type': 'application/json' },
            });
        }
        async getSwDashEventsCount() {
            return new Response(JSON.stringify({ error: 'Service worker not active', count: 0 }), {
                status: 503,
                headers: { 'Content-Type': 'application/json' },
            });
        }
        async getSwDashCumulativeMetrics() {
            return new Response(JSON.stringify({ error: 'Service worker not active', data: null }), {
                status: 503,
                headers: { 'Content-Type': 'application/json' },
            });
        }
        async getSwDashSpeedtest() {
            return new Response(JSON.stringify({ error: 'Service worker not active - speedtest unavailable' }), {
                status: 503,
                headers: { 'Content-Type': 'application/json' },
            });
        }
        async getSwDashBundle(ctx) {
            try {
                const bundleContent = (await plugins.fsInstance
                    .file(paths.swdashBundlePath)
                    .encoding('utf8')
                    .read());
                return new Response(bundleContent, {
                    status: 200,
                    headers: {
                        'Content-Type': 'text/javascript',
                        'Cache-Control': 'no-cache',
                    },
                });
            }
            catch (error) {
                console.error('Failed to serve sw-dash bundle:', error);
                return new Response('SW-Dash bundle not found', { status: 404 });
            }
        }
        async getServiceWorkerBundle() {
            try {
                const bundleContent = (await plugins.fsInstance
                    .file(paths.serviceworkerBundlePath)
                    .encoding('utf8')
                    .read());
                return new Response(bundleContent, {
                    status: 200,
                    headers: {
                        'Content-Type': 'text/javascript',
                        'Cache-Control': 'no-cache',
                    },
                });
            }
            catch (error) {
                console.error('Failed to serve serviceworker bundle:', error);
                return new Response('ServiceWorker bundle not found', { status: 404 });
            }
        }
    };
    return BuiltInRoutesController = _classThis;
})();
export { BuiltInRoutesController };
//# sourceMappingURL=data:application/json;base64, 