import { clone, getUUID } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
import { getGib, } from '@ibgib/ts-gib/dist/V1/index.mjs';
import { GLOBAL_LOG_A_LOT } from '../core-constants.mjs';
import { toDto } from '../common/other/ibgib-helper.mjs';
const logalot = GLOBAL_LOG_A_LOT || false;
export class LightWitnessBase_V1 {
    /**
     * Log context for convenience with logging. (Ignore if you don't want to use this.)
     */
    lc = `[${LightWitnessBase_V1.name}]`;
    instanceId = '';
    // #region IbGib interface fields: ib, gib, data, rel8ns
    /**
     * Used per use case in implementing class.
     *
     * This property is a simple property (no getter/setter with backing
     * fields).  This is to simplify usage with DTOs (Data Transfer Objects) for
     * storing in spaces.
     */
    ib = '';
    /**
     * Used per use case in implementing class.
     *
     * This property is a simple property (no getter/setter with backing
     * fields).  This is to simplify usage with DTOs (Data Transfer Objects) for
     * storing in spaces.
     */
    gib;
    /**
     * Used per use case in implementing class.
     *
     * This property is a simple property (no getter/setter with backing
     * fields).  This is to simplify usage with DTOs (Data Transfer Objects) for
     * storing in spaces.
     */
    data;
    /**
     * Used per use case in implementing class.
     *
     * This property is a simple property (no getter/setter with backing
     * fields).  This is to simplify usage with DTOs (Data Transfer Objects) for
     * storing in spaces.
     */
    rel8ns;
    // #endregion IbGib interface fields: ib, gib, data, rel8ns
    /**
     * await this to ensure the witness is ready.
     *
     * This is called in the base `Witness.witness` function before `witnessImpl`.
     */
    initialized;
    /**
     * synchronous and faster boolean for checking faster.
     *
     * so normally, do a check like...
     *    if (!this._isInitialized) { await this.initialized; }
     */
    _isInitialized = false;
    constructor(initialData, initialRel8ns) {
        if (initialData) {
            this.data = initialData;
        }
        if (initialRel8ns) {
            this.rel8ns = initialRel8ns;
        }
        this.initialized = this.initialize().then(() => { this._isInitialized = true; });
    }
    /**
     * by default, simply returns true atow in base class.
     * override this to perform code before any other code is executed.
     */
    async initialize() {
        this.instanceId = await getUUID();
        this.gib = await getGib({ ibGib: this.toIbGibDto() });
    }
    /**
     * Creates a data transfer object (dto) snapshot out of this
     * witness' `ib`, `gib`, `data` and `rel8ns` properties.
     *
     * I say "snapshot" because this copies each property
     * (`ib`, `gib`, `data`, `rel8ns`).
     *
     * ## thoughts
     *
     * Witness classes need to be able to persist their ibgib
     * just as regular data. But witnesses have the additional
     * layer of behavior (e.g. the `witness` function) that
     * will not persist (until we get more integrated version control
     * types of functionality in ibgib).
     *
     * @returns dto ibgib object with just clones of this.ib/gib/data/rel8ns props.
     *
     * @see {loadIbGibDto}
     */
    toIbGibDto() {
        return toDto({ ibGib: this });
    }
    /**
     * (Re)hydrates this witness class with the ibgib information from the dto.
     *
     * ## notes
     *
     * * You can extend this function for witness-specific behavior when loading.
     *
     * @param dto ib, gib, data & rel8ns to load for this witness ibgib instance.
     *
     * @see {toIbGibDto}
     */
    loadIbGibDto(dto) {
        const lc = `${this.lc}[${this.loadIbGibDto.name}]`;
        try {
            if (logalot) {
                console.log(`${lc} starting...`);
            }
            if (!dto.ib) {
                console.warn(`${lc} dto.ib is falsy.`);
            }
            if (!dto.gib) {
                console.warn(`${lc} dto.gib is falsy.`);
            }
            this.ib = clone(dto.ib);
            this.gib = clone(dto.gib);
            if (dto.data) {
                this.data = clone(dto.data);
            }
            else {
                delete this.data;
            }
            if (dto.rel8ns) {
                this.rel8ns = clone(dto.rel8ns);
            }
            else {
                delete this.rel8ns;
            }
            return Promise.resolve();
        }
        catch (error) {
            console.error(`${lc} ${error.message}`);
            throw error;
        }
        finally {
            if (logalot) {
                console.log(`${lc} complete.`);
            }
        }
    }
    /**
     * Validate this witness object, checking its own `data` and `rel8ns`, and
     * possibly other state.
     *
     * ## notes
     *
     * ATOW base implementation of this just checks for non-falsy
     * `this.ib` and `this.gib`
     */
    async validateThis() {
        const lc = `${this.lc}[${this.validateThis.name}]`;
        const errors = [];
        try {
            if (!this.ib) {
                errors.push(`this.ib is falsy.`);
            }
            if (!this.gib) {
                errors.push(`this.gib is falsy.`);
            }
        }
        catch (error) {
            console.error(`${lc} ${error.message}`);
            throw error;
        }
        return errors;
    }
}
//# sourceMappingURL=light-witness-base-v1.mjs.map