import { UmbArrayState } from '../../../../libs/observable-api/index.js';
import { UmbVariantId } from '../../variant/index.js';
const UBM_VARIANT_DELIMITER = '_&_';
/**
 * @class UmbWorkspaceSplitViewManager
 * @description - Class managing the split view state for a workspace context.
 */
export class UmbWorkspaceSplitViewManager {
    constructor() {
        this.#activeVariantsInfo = new UmbArrayState([], (x) => x.index).sortBy((a, b) => (a.index || 0) - (b.index || 0));
        this.activeVariantsInfo = this.#activeVariantsInfo.asObservable();
        this.firstActiveVariantInfo = this.#activeVariantsInfo.asObservablePart((x) => x[0]);
        this.splitViewActive = this.#activeVariantsInfo.asObservablePart((x) => x.length > 1);
    }
    #activeVariantsInfo;
    getWorkspaceRoute() {
        return this._routeBase;
    }
    setWorkspaceRoute(route) {
        this._routeBase = route;
    }
    setActiveVariant(index, culture, segment) {
        this.#activeVariantsInfo.appendOneAt({ index, culture: culture ?? null, segment: segment ?? null }, index);
    }
    getActiveVariants() {
        return this.#activeVariantsInfo.getValue();
    }
    removeActiveVariant(index) {
        if (this.getActiveVariants().length > 1) {
            this.#activeVariantsInfo.removeOne(index);
        }
    }
    activeVariantByIndex(index) {
        return this.#activeVariantsInfo.asObservablePart((data) => data.find((x) => x.index === index) || undefined);
    }
    switchVariant(index, variantId) {
        // TODO: remember current path and extend url with it.
        // TODO: construct URl with all active routes:
        // TODO: use method for generating variant url:
        const workspaceRoute = this.getWorkspaceRoute();
        if (workspaceRoute) {
            const activeVariants = this.getActiveVariants();
            if (activeVariants && index < activeVariants.length) {
                const newVariants = [...activeVariants];
                newVariants[index] = { index, culture: variantId.culture, segment: variantId.segment };
                const variantPart = newVariants
                    .map((v) => UmbVariantId.Create(v).toString())
                    .join(UBM_VARIANT_DELIMITER);
                const additionalPathname = this.#getAdditionalPathname();
                history.pushState(null, '', `${workspaceRoute}/${variantPart}${additionalPathname}`);
                return true;
            }
        }
        return false;
    }
    openSplitView(newVariant) {
        // TODO: remember current path and extend url with it.
        // TODO: construct URl with all active routes:
        // TODO: use method for generating variant url:
        const currentVariant = this.getActiveVariants()[0];
        const workspaceRoute = this.getWorkspaceRoute();
        if (currentVariant && workspaceRoute) {
            history.pushState(null, '', `${workspaceRoute}/${UmbVariantId.Create(currentVariant)}${UBM_VARIANT_DELIMITER}${newVariant}`);
            return true;
        }
        return false;
    }
    closeSplitView(index) {
        const workspaceRoute = this.getWorkspaceRoute();
        if (workspaceRoute) {
            const activeVariants = this.getActiveVariants();
            if (activeVariants && index < activeVariants.length) {
                const newVariants = activeVariants.filter((x) => x.index !== index);
                const variantPart = newVariants.map((v) => UmbVariantId.Create(v)).join(UBM_VARIANT_DELIMITER);
                history.pushState(null, '', `${workspaceRoute}/${variantPart}`);
                return true;
            }
        }
        return false;
    }
    setVariantParts(routeFragment) {
        const variantSplit = routeFragment.split(UBM_VARIANT_DELIMITER);
        variantSplit.forEach((part, index) => {
            this.handleVariantFolderPart(index, part);
        });
    }
    handleVariantFolderPart(index, folderPart) {
        const variantId = UmbVariantId.FromString(folderPart);
        this.setActiveVariant(index, variantId.culture, variantId.segment);
    }
    #getCurrentVariantPathname() {
        const workspaceRoute = this.getWorkspaceRoute();
        const activeVariants = this.getActiveVariants();
        const currentVariantPart = activeVariants
            .map((v) => UmbVariantId.Create(v).toString())
            .join(UBM_VARIANT_DELIMITER);
        return `${workspaceRoute}/${currentVariantPart}`;
    }
    #getAdditionalPathname() {
        const currentUrl = new URL(window.location.href);
        const currentFullPathname = currentUrl.pathname;
        const currentVariantPathname = this.#getCurrentVariantPathname();
        if (currentVariantPathname && currentFullPathname.startsWith(currentVariantPathname)) {
            return currentFullPathname.substring(currentVariantPathname.length);
        }
        // If the currentVariantPathname is not a prefix, return empty string
        return '';
    }
}
