"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MablAuthoringService = void 0;
const validator_1 = require("../validator");
const authoringGuideResources_1 = require("./authoringGuideResources");
const examplesLoader_1 = require("./examplesLoader");
const suggestions_1 = require("./suggestions");
const enrichErrors_1 = require("./enrichErrors");
const schemaResources_1 = require("./schemaResources");
class MablAuthoringService {
    examples;
    suggestions;
    browserSchemas;
    mobileSchemas;
    authoringGuide;
    constructor(deps) {
        this.examples = deps?.examples ?? examplesLoader_1.DEFAULT_EXAMPLES;
        this.suggestions = deps?.suggestions ?? suggestions_1.DEFAULT_SUGGESTIONS;
        this.browserSchemas = new schemaResources_1.SchemaResources(this.examples, 'browser');
        this.mobileSchemas = new schemaResources_1.SchemaResources(this.examples, 'mobile');
        this.authoringGuide = new authoringGuideResources_1.AuthoringGuideResources();
    }
    validate(jsonSteps, options) {
        const raw = (0, validator_1.validate)(jsonSteps, options);
        return (0, enrichErrors_1.enrichValidationResult)({
            result: raw,
            jsonSteps,
            testType: options?.testType,
            suggestions: this.suggestions,
            examples: this.examples,
        });
    }
    getSchemaResource(uri, testType = 'browser') {
        const resources = testType === 'mobile' ? this.mobileSchemas : this.browserSchemas;
        return resources.resolve(uri);
    }
    listSchemaResources(testType = 'browser') {
        const resources = testType === 'mobile' ? this.mobileSchemas : this.browserSchemas;
        return resources.listResources();
    }
    listExamples(filter = {}) {
        const search = filter.search?.toLowerCase().trim();
        return Object.values(this.examples)
            .filter((example) => matchesFilter(example, filter, search))
            .map(({ id, title, tags, appliesTo }) => ({ id, title, tags, appliesTo }));
    }
    getExample(id) {
        return this.examples[id];
    }
    listExampleResources() {
        return Object.values(this.examples).map((example) => ({
            uri: `examples://${example.id}`,
            description: example.title,
        }));
    }
    getAuthoringGuide(uri) {
        return this.authoringGuide.resolve(uri);
    }
    listAuthoringGuideResources() {
        return this.authoringGuide.list();
    }
}
exports.MablAuthoringService = MablAuthoringService;
function matchesFilter(example, filter, searchLower) {
    if (filter.appliesTo && !example.appliesTo.includes(filter.appliesTo)) {
        return false;
    }
    if (filter.tags?.length) {
        const exampleTagSet = new Set(example.tags);
        if (!filter.tags.some((tag) => exampleTagSet.has(tag))) {
            return false;
        }
    }
    if (searchLower) {
        const haystack = [example.id, example.title, ...example.tags]
            .join(' ')
            .toLowerCase();
        if (!haystack.includes(searchLower))
            return false;
    }
    return true;
}
