var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { UmbDocumentItemRepository } from '../../item/index.js';
import { UMB_CREATE_DOCUMENT_WORKSPACE_PATH_PATTERN, UMB_CREATE_FROM_BLUEPRINT_DOCUMENT_WORKSPACE_PATH_PATTERN, } from '../../paths.js';
import { html, customElement, state, repeat, css, when } from '../../../../../external/lit/index.js';
import { UmbTextStyles } from '../../../../core/style/index.js';
import { UmbModalBaseElement } from '../../../../core/modal/index.js';
import { UmbDocumentTypeStructureRepository, } from '../../../document-types/index.js';
import { UmbDocumentBlueprintItemRepository, } from '../../../document-blueprints/index.js';
let UmbDocumentCreateOptionsModalElement = class UmbDocumentCreateOptionsModalElement extends UmbModalBaseElement {
    constructor() {
        super(...arguments);
        this.#documentTypeStructureRepository = new UmbDocumentTypeStructureRepository(this);
        this.#documentItemRepository = new UmbDocumentItemRepository(this);
        this.#documentBlueprintItemRepository = new UmbDocumentBlueprintItemRepository(this);
        this.#documentTypeUnique = '';
        this.#documentTypeIcon = '';
        this._allowedDocumentTypes = [];
        this._headline = `${this.localize.term('create_createUnder')} ${this.localize.term('actionCategories_content')}`;
        this._availableBlueprints = [];
    }
    #documentTypeStructureRepository;
    #documentItemRepository;
    #documentBlueprintItemRepository;
    #documentTypeUnique;
    #documentTypeIcon;
    async firstUpdated() {
        const parentUnique = this.data?.parent.unique;
        const documentTypeUnique = this.data?.documentType?.unique || null;
        this.#retrieveAllowedDocumentTypesOf(documentTypeUnique, parentUnique || null);
        if (parentUnique) {
            this.#retrieveHeadline(parentUnique);
        }
    }
    async #retrieveAllowedDocumentTypesOf(unique, parentContentUnique) {
        const { data } = await this.#documentTypeStructureRepository.requestAllowedChildrenOf(unique, parentContentUnique);
        if (data) {
            // TODO: implement pagination, or get 1000?
            this._allowedDocumentTypes = data.items;
        }
    }
    async #retrieveHeadline(parentUnique) {
        if (!parentUnique)
            return;
        const { data } = await this.#documentItemRepository.requestItems([parentUnique]);
        if (data) {
            // TODO: we need to get the correct variant context here
            this._headline = `${this.localize.term('create_createUnder')} ${data[0].variants?.[0].name ?? this.localize.term('actionCategories_content')}`;
        }
    }
    // close the modal when navigating to data type
    #onNavigate(documentTypeUnique, blueprintUnique) {
        if (!this.data) {
            throw new Error('Data is not defined');
        }
        if (!blueprintUnique) {
            history.pushState(null, '', UMB_CREATE_DOCUMENT_WORKSPACE_PATH_PATTERN.generateAbsolute({
                parentEntityType: this.data.parent.entityType,
                parentUnique: this.data.parent.unique,
                documentTypeUnique,
            }));
        }
        else {
            history.pushState(null, '', UMB_CREATE_FROM_BLUEPRINT_DOCUMENT_WORKSPACE_PATH_PATTERN.generateAbsolute({
                parentEntityType: this.data.parent.entityType,
                parentUnique: this.data.parent.unique,
                documentTypeUnique,
                blueprintUnique,
            }));
        }
        this._submitModal();
    }
    async #onSelectDocumentType(documentTypeUnique) {
        if (!documentTypeUnique) {
            throw new Error('Document type unique is not defined');
        }
        this.#documentTypeUnique = documentTypeUnique;
        this.#documentTypeIcon = this._allowedDocumentTypes.find((dt) => dt.unique === documentTypeUnique)?.icon ?? '';
        const { data } = await this.#documentBlueprintItemRepository.requestItemsByDocumentType(documentTypeUnique);
        this._availableBlueprints = data ?? [];
        if (!this._availableBlueprints.length) {
            this.#onNavigate(documentTypeUnique);
            return;
        }
    }
    render() {
        const renderBlueprint = this._availableBlueprints.length && this.#documentTypeUnique;
        const headline = renderBlueprint ? this.localize.term('blueprints_selectBlueprint') : this._headline;
        return html `
			<uui-dialog-layout headline=${headline}>
				${when(renderBlueprint, () => this.#renderBlueprints(), () => this.#renderDocumentTypes())}
				<uui-button
					slot="actions"
					id="cancel"
					label=${this.localize.term('general_cancel')}
					@click="${this._rejectModal}"></uui-button>
			</uui-dialog-layout>
		`;
    }
    #renderNoDocumentTypes() {
        if (this.data?.documentType?.unique) {
            return html `
				<umb-localize key="create_noDocumentTypes">
					There are no allowed Document Types available for creating content here. You must enable these in
					<strong>Document Types</strong> within the <strong>Settings</strong> section, by editing the
					<strong>Allowed child node types</strong> under <strong>Structure</strong>.
				</umb-localize>
				<br />
				<uui-button
					id="edit-permissions"
					look="secondary"
					href=${`/section/settings/workspace/document-type/edit/${this.data?.documentType?.unique}/view/structure`}
					label=${this.localize.term('create_noDocumentTypesEditPermissions')}
					@click=${() => this._rejectModal()}></uui-button>
			`;
        }
        else {
            return html `
				<umb-localize key="create_noDocumentTypesAllowedAtRoot">
					There are no allowed Document Types available for creating content here. You must enable these in
					<strong>Document Types</strong> within the <strong>Settings</strong> section, by changing the
					<strong>Allow as root</strong> option under <strong>Structure</strong>.
				</umb-localize>
			`;
        }
    }
    #renderDocumentTypes() {
        return when(this._allowedDocumentTypes.length === 0, () => this.#renderNoDocumentTypes(), () => repeat(this._allowedDocumentTypes, (documentType) => documentType.unique, (documentType) => html `
						<uui-ref-node-document-type
							.name=${this.localize.string(documentType.name)}
							.alias=${this.localize.string(documentType.description ?? '')}
							select-only
							selectable
							@selected=${() => this.#onSelectDocumentType(documentType.unique)}
							@open=${() => this.#onSelectDocumentType(documentType.unique)}>
							<umb-icon slot="icon" name=${documentType.icon || 'icon-circle-dotted'}></umb-icon>
						</uui-ref-node-document-type>
					`));
    }
    #renderBlueprints() {
        return html `
			<uui-menu-item
				id="blank"
				label=${this.localize.term('blueprints_blankBlueprint')}
				@click=${() => this.#onNavigate(this.#documentTypeUnique)}>
				<umb-icon slot="icon" name=${this.#documentTypeIcon}></umb-icon>
			</uui-menu-item>
			${repeat(this._availableBlueprints, (blueprint) => blueprint.unique, (blueprint) => html `<uui-menu-item
						label=${blueprint.name}
						@click=${() => this.#onNavigate(this.#documentTypeUnique, blueprint.unique)}>
						<umb-icon slot="icon" name="icon-blueprint"></umb-icon>
					</uui-menu-item>`)}
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			#blank {
				border-bottom: 1px solid var(--uui-color-border);
			}

			#edit-permissions {
				margin-top: var(--uui-size-6);
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbDocumentCreateOptionsModalElement.prototype, "_allowedDocumentTypes", void 0);
__decorate([
    state()
], UmbDocumentCreateOptionsModalElement.prototype, "_headline", void 0);
__decorate([
    state()
], UmbDocumentCreateOptionsModalElement.prototype, "_availableBlueprints", void 0);
UmbDocumentCreateOptionsModalElement = __decorate([
    customElement('umb-document-create-options-modal')
], UmbDocumentCreateOptionsModalElement);
export { UmbDocumentCreateOptionsModalElement };
export default UmbDocumentCreateOptionsModalElement;
