"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.field = void 0;
exports.mostSpecificClassURI = mostSpecificClassURI;
exports.fieldFunction = fieldFunction;
const solid_logic_1 = require("solid-logic");
const log_1 = require("../../log");
const error_1 = require("../error");
const store = solid_logic_1.solidLogicSingleton.store;
exports.field = {}; // Form field functions by URI of field type.
/**
 * Which class of field is this? Relies on http://www.w3.org/2000/01/rdf-schema#subClassOf and
 * https://linkeddata.github.io/rdflib.js/doc/classes/formula.html#bottomtypeuris
 * to find the most specific RDF type if there are multiple.
 *
 * @param x a form field, e.g. `namedNode('https://timbl.com/timbl/Public/Test/Forms/individualForm.ttl#fullNameField')`
 * @returns the URI of the most specific known class, e.g. `http://www.w3.org/ns/ui#SingleLineTextField`
 */
function mostSpecificClassURI(x) {
    const kb = store;
    const ft = kb.findTypeURIs(x);
    const bot = kb.bottomTypeURIs(ft); // most specific
    const bots = [];
    for (const b in bot)
        bots.push(b);
    // if (bots.length > 1) throw "Didn't expect "+x+" to have multiple bottom types: "+bots
    return bots[0];
}
/**
 * Returns a function that creates a form widget
 * @param dom unused
 * @param fieldInQuestion the field for which to create a form, e.g. namedNode('https://timbl.com/timbl/Public/Test/Forms/individualForm.ttl#fullNameField')
 */
function fieldFunction(dom /* unused */, fieldInQuestion) {
    const uri = mostSpecificClassURI(fieldInQuestion); // What type
    // const uri = field.uri
    const fun = exports.field[uri];
    (0, log_1.debug)('paneUtils: Going to implement field ' + fieldInQuestion + ' of type ' + uri);
    if (!fun) {
        return function (dom2, container) {
            const box = (0, error_1.errorMessageBlock)(dom2, 'No handler for field ' + fieldInQuestion + ' of type ' + uri);
            if (container)
                container.appendChild(box);
            return box;
        };
    }
    return fun;
}
//# sourceMappingURL=fieldFunction.js.map