/**
 * @module dynamic-form-factory-base
 *
 *
 * @see {@link WitnessFactoryBase}
 */
import { patchObject } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
import { WitnessFactoryBase } from "./witness-factory-base.mjs";
import { GLOBAL_LOG_A_LOT } from "../../core-constants.mjs";
const logalot = GLOBAL_LOG_A_LOT || false;
/**
 * The idea is that any model can be injected via this factory provider.
 *
 * So when you create a witness that you want to be able to instantiate via
 * just metadata, you also provide an accompanying factory that knows
 * how to map from a
 *   * witness(model) -> form data (to generate dynamic forms)
 *   * form data -> witness (to instantiate witness from data)
 */
export class DynamicFormFactoryBase extends WitnessFactoryBase {
    lc = `[${DynamicFormFactoryBase.name}]`;
    /**
     * iterates through the given {@link items}.
     *
     * If the item has child items, it is considered a grouping (i.e. FormGroup
     * or FormArray) and this function is called recursively with the
     * {@link contextPath} adjusted per the {@link pathDelimiter} and
     * `item.name`.
     *
     * If the item does not have child items, it's considered to be a setting at
     * the current path level and it's value is assigned per its `item.name` as
     * the key.
     */
    patchDataFromItems({ data, contextPath, items, pathDelimiter, }) {
        const lc = `${this.lc}[${this.patchDataFromItems.name}]`;
        try {
            if (logalot) {
                console.log(`${lc} starting...`);
            }
            if (!pathDelimiter) {
                throw new Error(`pathDelimiter required (E: 958d472a15fb71e45cd2925883f2ec22)`);
            }
            for (let i = 0; i < items.length; i++) {
                const item = items[i];
                const path = contextPath ?
                    contextPath + pathDelimiter + item.name :
                    item.name;
                if (!item.items) {
                    // it's a property (FormControl, not FormGroup/Array)
                    patchObject({
                        obj: data,
                        value: item.value,
                        path,
                        logalot,
                        pathDelimiter,
                    });
                }
                else if (item.items.length > 0) {
                    // it's a group, so call this function recursively
                    this.patchDataFromItems({
                        data,
                        contextPath: path,
                        items: item.items,
                        pathDelimiter,
                    });
                }
                else {
                    throw new Error(`invalid item. items is truthy but with a length of 0. items should either be falsy or have at least one child. (E: ee2765e0b920477f919fcb09e9d951b4)`);
                }
            }
        }
        catch (error) {
            console.error(`${lc} ${error.message}`);
            throw error;
        }
        finally {
            if (logalot) {
                console.log(`${lc} complete.`);
            }
        }
    }
}
//# sourceMappingURL=dynamic-form-factory-base.mjs.map