import Structure from "../Structure";
import StructureFactory, {StructureImplConfig} from "../StructureFactory";

export default class Basic extends Structure{
    constructor(params: Partial<Basic> = {}) {
        super(params);
        Object.assign(this, params);
    }

    public placeHtml(container: HTMLElement): void {
        const structureDiv = document.createElement("div");
        this.addStandardContainerProperties(structureDiv);
        container.append(structureDiv);
        this.placedHtml.push(structureDiv);
        this.contents.forEach(c => c.placeHtml(structureDiv));
    }
}

export function addBasicToFactory(factory: StructureFactory): void {
    factory.registerConstructor("basic", (config, params) => {
        const impl = config.impl;
        if (!isBasicConfig(impl)) return null;

        const groupParam: Partial<Basic> = params as Partial<Basic>;

        return new Basic(groupParam);
    });
}

function isBasicConfig(config:StructureImplConfig): config is BasicConfig{
    return config.type === "basic";
}

interface BasicConfig extends StructureImplConfig{
    type: "basic";
}