"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Unit = void 0;
const element_js_1 = require("../element.js");
const entity_js_1 = require("./entity.js");
const module_js_1 = require("./module.js");
const params_js_1 = require("./params.js");
const pipeline_js_1 = require("./pipeline.js");
class Unit extends entity_js_1.Entity {
    static get $metadata() {
        const module = this;
        if (!module.hasOwnProperty('_meta')) {
            module._meta = (0, module_js_1.parseModuleMetadata)(module);
        }
        return module._meta;
    }
    get $class() { return this.constructor; }
    get $context() { return this.$action.$context; }
    get $script() { return this.$context.$script; }
    get $engine() { return this.$script.$engine; }
    get $browser() { return this.$script.$browser; }
    get $page() { return this.$script.$page; }
    get $container() {
        const ext = this.$class.$extension;
        return ext.container;
    }
    toJSON() {
        return {
            ...super.toJSON(),
            type: this.type,
        };
    }
    getParams() {
        return (0, params_js_1.getAllParams)(this.constructor.prototype);
    }
    deserializeParam(param, value) {
        switch (param.type) {
            case 'string':
            case 'enum':
            case 'selector':
            case 'template':
            case 'json':
            case 'javascript':
            case 'definition':
                return String(value || '');
            case 'keys':
                return Array.isArray(value) ? value.map(String) : [];
            case 'recordset': {
                const records = [];
                const array = Array.isArray(value) ? value : [];
                for (const row of array) {
                    const record = {};
                    for (const field of param.fields) {
                        const val = this.deserializeParam(field, row[field.name]);
                        record[field.name] = val;
                    }
                    records.push(record);
                }
                return records;
            }
            case 'boolean':
                return Boolean(value || false);
            case 'number': {
                if (value == null) {
                    return null;
                }
                const num = parseFloat(value);
                return isNaN(num) ? null : num;
            }
            case 'pipeline':
                return new pipeline_js_1.Pipeline(this, param.name, value || {});
            default:
                return value;
        }
    }
    readParams(spec) {
        for (const param of this.getParams()) {
            if (!param.serialized) {
                continue;
            }
            const val = [
                spec[param.name],
                param.value,
                this[param.name]
            ].find(_ => _ != null) ?? null;
            this[param.name] = this.deserializeParam(param, val);
        }
    }
    getParamValue(name) {
        return this[name];
    }
    async createDocument(value = {}) {
        const document = await this.$page.document();
        return new element_js_1.Element(document, value);
    }
}
exports.Unit = Unit;
Unit.$help = '';
//# sourceMappingURL=unit.js.map