import { simpleHashCode, UmbArrayState } from '../../../../libs/observable-api/index.js';
import { UmbControllerBase } from '../../../../libs/class-api/index.js';
import { UMB_INTERACTION_MEMORY_CONTEXT } from '../../interaction-memory/index.js';
export class UmbPropertyEditorUiInteractionMemoryManager extends UmbControllerBase {
    #memories;
    #interactionMemoryContext;
    #configHashCode;
    #memoryUniquePrefix;
    #init;
    constructor(host, args) {
        super(host);
        this.#memories = new UmbArrayState([], (x) => x.unique);
        this.memoriesForPropertyEditor = this.#memories.asObservable();
        this.#memoryUniquePrefix = args.memoryUniquePrefix;
        this.#init = Promise.all([
            this.consumeContext(UMB_INTERACTION_MEMORY_CONTEXT, (context) => {
                this.#interactionMemoryContext = context;
            }).asPromise(),
        ]);
    }
    /**
     * Sets the property editor config, used to create a unique hash for the interaction memory.
     * @param {(UmbPropertyEditorConfigCollection | undefined)} config
     * @memberof UmbPropertyEditorUiInteractionMemoryManager
     */
    setPropertyEditorConfig(config) {
        this.#setConfigHash(config);
        this.#getInteractionMemory();
    }
    /**
     * Creates or updates an interaction memory for this property editor based on the provided memories.
     * @param {Array<UmbInteractionMemoryModel>} memories - The memories to include for this property editor.
     * @returns {Promise<void>}
     * @memberof UmbPropertyEditorUiInteractionMemoryManager
     */
    async saveMemoriesForPropertyEditor(memories) {
        await this.#init;
        const memoryUnique = this.#getInteractionMemoryUnique();
        if (!this.#interactionMemoryContext)
            return;
        const propertyEditorMemory = {
            unique: memoryUnique,
            memories,
        };
        this.#interactionMemoryContext.memory.setMemory(propertyEditorMemory);
        this.#memories.setValue(memories);
    }
    /**
     * Deletes the interaction memory for this property editor.
     * @memberof UmbPropertyEditorUiInteractionMemoryManager
     */
    async deleteMemoriesForPropertyEditor() {
        await this.#init;
        const unique = this.#getInteractionMemoryUnique();
        this.#interactionMemoryContext?.memory.deleteMemory(unique);
        this.#memories.setValue([]);
    }
    #getInteractionMemoryUnique() {
        return `${this.#memoryUniquePrefix}PropertyEditorUi${this.#configHashCode ? '-' + this.#configHashCode : ''}`;
    }
    async #getInteractionMemory() {
        await this.#init;
        const memoryUnique = this.#getInteractionMemoryUnique();
        if (!memoryUnique)
            return;
        if (!this.#interactionMemoryContext)
            return;
        const memory = this.#interactionMemoryContext.memory.getMemory(memoryUnique);
        this.#memories.setValue(memory?.memories ?? []);
    }
    #setConfigHash(config) {
        const configString = config ? JSON.stringify(config.toObject()) : '';
        const hashCode = simpleHashCode(configString);
        this.#configHashCode = hashCode;
    }
}
