import { ElementFactory } from "../Page/ElementsOfFormFactory/ElementFactory";
import { IMainStateFactory } from "../Types";
import { Cach } from "./Cach";

export class PayLoadBoxFactory extends ElementFactory {
    public forceUpdate = () => { };
    #value: any;
    public get value() {
        return this.#value;
    }

    public setValue = (value: any) => {
        this.#value = value;
        Cach.setValue(this, value);
    };
    public deseriallize = (e: any) => {
        this.clearData();
        if (e === null) {
            //do nothing
        } else if (e === undefined) {
            //do nothing
        } else {
            this.#value = e;
            this.#defaultValue = this.#value;
        }

        Cach.setValue(this, this.#value);
    }

    public clearData = () => {
        Cach.clear(this);
        this.#value = undefined;
        this.#defaultValue = undefined;
        this.validation = true;
        this._hasChange = false;
    }

    #defaultValue: any;
    public get defaultValue() {
        return this.#defaultValue;
    }

    public focusToElement = () => {
    }

    public tabIndex = undefined;

    constructor(
        _mainStateFactory: IMainStateFactory,
        _fieldName: string,
        _dispose: () => void,
        initialValue?: boolean,
        payLoadKey?: string,
        responseKey?: string,
    ) {
        super(_mainStateFactory, _fieldName, _dispose, payLoadKey, responseKey);

        this.#defaultValue = initialValue;
        this.#value = initialValue;

        if (Cach.isCached(this)) {
            const cach = Cach.getCached(this);
            this.#value = cach.value;
        }
    }

    public restartDefaultValue = () => {
        this.#defaultValue = this.value;
        this.refreshHasChange();
    };

    public refreshHasChange = () => {
    };

    public validate = () => {
        this.validation = true;
    }
}