import { UmbId } from '../../../core/id/index.js';
export class UmbBlockPropertyValueCloner {
    #contentIdUpdatedCallback;
    #propertyEditorAlias;
    #contentData;
    #settingsData;
    #expose;
    constructor(propertyEditorAlias, args) {
        this.#propertyEditorAlias = propertyEditorAlias;
        this.#contentIdUpdatedCallback = args?.contentIdUpdatedCallback;
    }
    async cloneValue(value) {
        if (value) {
            this.#contentData = value.contentData;
            this.#settingsData = value.settingsData;
            this.#expose = value.expose;
            const result = {
                ...value,
                layout: {
                    [this.#propertyEditorAlias]: await this._cloneLayout(value.layout[this.#propertyEditorAlias]),
                },
                contentData: this.#contentData,
                settingsData: this.#settingsData,
                expose: this.#expose,
            };
            return result;
        }
        return value;
    }
    async _cloneBlock(layoutEntry) {
        const clonedLayoutEntry = { ...layoutEntry };
        const contentKey = layoutEntry.contentKey;
        const settingsKey = layoutEntry.settingsKey;
        // Generate new contentKey and settingsKey:
        const newContentKey = UmbId.new();
        clonedLayoutEntry.contentKey = newContentKey;
        // Replace contentKeys in contentData
        this.#contentData = this.#contentData?.map((contentData) => {
            if (contentData.key === contentKey) {
                return { ...contentData, key: newContentKey };
            }
            return contentData;
        });
        // Replace contentKey in expose:
        this.#expose = this.#expose?.map((expose) => {
            if (expose.contentKey === contentKey) {
                return { ...expose, contentKey: newContentKey };
            }
            return expose;
        });
        this.#contentIdUpdatedCallback?.(contentKey, newContentKey);
        if (settingsKey) {
            const newSettingsKey = UmbId.new();
            clonedLayoutEntry.settingsKey = newSettingsKey;
            // Replace settingsKeys in settingsData
            this.#settingsData = this.#settingsData?.map((settingsData) => {
                if (settingsData.key === settingsKey) {
                    return { ...settingsData, key: newSettingsKey };
                }
                return settingsData;
            });
        }
        return clonedLayoutEntry;
    }
    destroy() { }
}
