import { UMB_VARIANT_WORKSPACE_CONTEXT } from '../../contexts/index.js';
import { UmbContextToken } from '../../../../../libs/context-api/index.js';
import { UmbContextBase } from '../../../../../libs/class-api/index.js';
import { UmbBooleanState, UmbClassState, UmbNumberState } from '../../../../../libs/observable-api/index.js';
import { UmbVariantId } from '../../../variant/index.js';
import { UMB_MARK_ATTRIBUTE_NAME } from '../../../const/index.js';
export class UmbWorkspaceSplitViewContext extends UmbContextBase {
    //
    #variantVariantValidationContext;
    #workspaceContext;
    getWorkspaceContext() {
        return this.#workspaceContext;
    }
    #datasetContext;
    #index;
    #isNew;
    #variantId;
    constructor(host) {
        super(host, UMB_WORKSPACE_SPLIT_VIEW_CONTEXT);
        this.#index = new UmbNumberState(undefined);
        this.index = this.#index.asObservable();
        this.#isNew = new UmbBooleanState(undefined);
        this.isNew = this.#isNew.asObservable();
        this.#variantId = new UmbClassState(undefined);
        this.variantId = this.#variantId.asObservable();
        this.consumeContext(UMB_VARIANT_WORKSPACE_CONTEXT, (context) => {
            this.#workspaceContext = context;
            this.#observeVariant();
            this.#observeIsNew();
        });
        this.observe(this.index, () => {
            this.#observeVariant();
        }, null);
    }
    #observeIsNew() {
        this.observe(this.#workspaceContext?.isNew, (isNew) => {
            this.#isNew.setValue(isNew ?? false);
        }, 'umbObserveIsNew');
    }
    #observeVariant() {
        if (!this.#workspaceContext)
            return;
        const index = this.#index.getValue();
        if (index === undefined)
            return;
        this.observe(this.#workspaceContext.splitView.activeVariantByIndex(index), async (activeVariantInfo) => {
            if (this.#variantVariantValidationContext) {
                this.#variantVariantValidationContext.unprovide();
            }
            if (!activeVariantInfo)
                return;
            this.#datasetContext?.destroy();
            const variantId = UmbVariantId.Create(activeVariantInfo);
            this.#variantId.setValue(variantId);
            const validationContext = this.#workspaceContext?.getVariantValidationContext(variantId);
            if (validationContext) {
                validationContext.provideAt(this);
                this.#variantVariantValidationContext = validationContext;
            }
            this.#datasetContext = this.#workspaceContext?.createPropertyDatasetContext(this, variantId);
            this.getHostElement().setAttribute(UMB_MARK_ATTRIBUTE_NAME, 'workspace-split-view:' + variantId.toString());
        }, '_observeActiveVariant');
    }
    switchVariant(variant) {
        const index = this.#index.value;
        if (index === undefined)
            return;
        this.#workspaceContext?.splitView.switchVariant(index, variant);
    }
    closeSplitView() {
        const index = this.#index.value;
        if (index === undefined)
            return;
        this.#workspaceContext?.splitView.closeSplitView(index);
    }
    openSplitView(variant) {
        this.#workspaceContext?.splitView.openSplitView(variant);
    }
    getSplitViewIndex() {
        return this.#index.getValue();
    }
    setSplitViewIndex(index) {
        this.#index.setValue(index);
    }
    /**
     *
     * concept this class could have methods to set and get the culture and segment of the active variant? just by using the index.
     */
    destroy() {
        this.#isNew.destroy();
        this.#variantId.destroy();
        this.#index.destroy();
        this.#variantVariantValidationContext?.unprovide();
        this.#datasetContext?.destroy();
        this.#workspaceContext = undefined;
        this.#variantVariantValidationContext = undefined;
        this.#datasetContext = undefined;
        super.destroy();
    }
}
export const UMB_WORKSPACE_SPLIT_VIEW_CONTEXT = new UmbContextToken('UmbWorkspaceSplitViewContext');
