import { UMB_PARTIAL_VIEW_ENTITY_TYPE } from '../entity.js';
import { UMB_PARTIAL_VIEW_DETAIL_REPOSITORY_ALIAS } from '../constants.js';
import { UmbPartialViewWorkspaceEditorElement } from './partial-view-workspace-editor.element.js';
import { UMB_PARTIAL_VIEW_WORKSPACE_ALIAS } from './manifests.js';
import { UmbEntityNamedDetailWorkspaceContextBase, UmbWorkspaceIsNewRedirectController, } from '../../../core/workspace/index.js';
import { tryExecute } from '../../../core/resources/index.js';
import { PartialViewService } from '../../../core/backend-api/index.js';
import { UmbServerFileRenameWorkspaceRedirectController } from '../../../core/server-file-system/index.js';
export class UmbPartialViewWorkspaceContext extends UmbEntityNamedDetailWorkspaceContextBase {
    constructor(host) {
        super(host, {
            workspaceAlias: UMB_PARTIAL_VIEW_WORKSPACE_ALIAS,
            entityType: UMB_PARTIAL_VIEW_ENTITY_TYPE,
            detailRepositoryAlias: UMB_PARTIAL_VIEW_DETAIL_REPOSITORY_ALIAS,
        });
        this.content = this._data.createObservablePartOfCurrent((data) => data?.content);
        this.#onCreate = async (args) => {
            await this.createScaffold(args);
            new UmbWorkspaceIsNewRedirectController(this, this, this.getHostElement().shadowRoot.querySelector('umb-router-slot'));
        };
        this.routes.setRoutes([
            {
                path: 'create/parent/:entityType/:parentUnique/snippet/:snippetId',
                component: UmbPartialViewWorkspaceEditorElement,
                setup: async (component, info) => {
                    const parentEntityType = info.match.params.entityType;
                    const parentUnique = info.match.params.parentUnique === 'null' ? null : info.match.params.parentUnique;
                    const snippetId = info.match.params.snippetId;
                    this.#onCreate({
                        parent: { entityType: parentEntityType, unique: parentUnique },
                        snippet: { unique: snippetId },
                    });
                },
            },
            {
                path: 'create/parent/:entityType/:parentUnique',
                component: UmbPartialViewWorkspaceEditorElement,
                setup: async (component, info) => {
                    const parentEntityType = info.match.params.entityType;
                    const parentUnique = info.match.params.parentUnique === 'null' ? null : info.match.params.parentUnique;
                    this.#onCreate({
                        parent: { entityType: parentEntityType, unique: parentUnique },
                        snippet: null,
                    });
                },
            },
            {
                path: 'edit/:unique',
                component: UmbPartialViewWorkspaceEditorElement,
                setup: (component, info) => {
                    const unique = info.match.params.unique;
                    this.load(unique);
                    new UmbServerFileRenameWorkspaceRedirectController(this, this, this.getHostElement().shadowRoot.querySelector('umb-router-slot'));
                },
            },
        ]);
    }
    #onCreate;
    setContent(value) {
        this._data.updateCurrent({ content: value });
    }
    async createScaffold(args) {
        let snippetContent = '';
        if (args.snippet?.unique) {
            const { data: snippet } = await this.#getSnippet(args.snippet.unique);
            snippetContent = snippet?.content || '';
        }
        const argsWithPreset = { ...args, preset: { content: snippetContent } };
        return super.createScaffold(argsWithPreset);
    }
    #getSnippet(unique) {
        return tryExecute(this, PartialViewService.getPartialViewSnippetById({
            path: { id: unique },
        }));
    }
}
export { UmbPartialViewWorkspaceContext as api };
