import { __esDecorate, __runInitializers } from "tslib";
import { html } from 'lit';
import { property } from 'lit/decorators.js';
import { Events } from '../../events/Events.js';
import AppNavigation from '../navigation/internals/AppNavigationElement.js';
import { ProjectFolderKind } from '@api-client/core/models/kinds.js';
import { Kind as ProjectRequestKind } from '@api-client/core/models/ProjectRequest.js';
import { Kind as EnvironmentKind } from '@api-client/core/models/Environment.js';
let ProjectNavigation = (() => {
    let _classSuper = AppNavigation;
    let _project_decorators;
    let _project_initializers = [];
    let _project_extraInitializers = [];
    return class ProjectNavigation extends _classSuper {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _project_decorators = [property({ type: Object })];
            __esDecorate(this, null, _project_decorators, { kind: "accessor", name: "project", static: false, private: false, access: { has: obj => "project" in obj, get: obj => obj.project, set: (obj, value) => { obj.project = value; } }, metadata: _metadata }, _project_initializers, _project_extraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #project_accessor_storage = __runInitializers(this, _project_initializers, void 0);
        /**
         * The instance of the project to create the navigation for.
         */
        get project() { return this.#project_accessor_storage; }
        set project(value) { this.#project_accessor_storage = value; }
        /**
         * Computed value. Returns true when the project has any items to render in the view.
         */
        get hasItems() {
            const { project } = this;
            if (!project) {
                return false;
            }
            // the HttpProject doesn't do any computations when folder is not set. It's just a copy of the source array.
            const folders = project.listFolders();
            const requests = project.listRequests();
            const environments = project.listEnvironments();
            return !!folders.length || !!requests.length || !!environments.length;
        }
        constructor() {
            super();
            __runInitializers(this, _project_extraInitializers);
            this.noVisualSelection = true;
        }
        connectedCallback() {
            super.connectedCallback();
            this.setAttribute('aria-label', 'HTTP project');
        }
        willUpdate(cp) {
            if (cp.has('project') && this.project) {
                this.label = this.project.info.renderLabel;
            }
            super.willUpdate(cp);
        }
        setupDataTransfer(item, dt) {
            const { project } = this;
            if (!project) {
                return;
            }
            super.setupDataTransfer(item, dt);
            dt.setData('text/httpproject', project.key);
        }
        commitName(key, kind, name) {
            const { project } = this;
            if (!project) {
                return;
            }
            if (!name) {
                return;
            }
            if (kind === ProjectFolderKind) {
                const object = project.findFolder(key);
                if (!object) {
                    throw new Error(`Invalid state. Folder not found.`);
                }
                object.info.name = name;
            }
            else if (kind === ProjectRequestKind) {
                const object = project.findRequest(key);
                if (!object) {
                    throw new Error(`Invalid state. Request not found.`);
                }
                object.info.name = name;
            }
            else if (kind === EnvironmentKind) {
                const object = project.findEnvironment(key);
                if (!object) {
                    throw new Error(`Invalid state. Environment not found.`);
                }
                object.info.name = name;
            }
            else {
                throw new Error(`Invalid state. Unknown kind.`);
            }
            this.edited = undefined;
            Events.HttpProject.changed(this);
            Events.HttpProject.State.nameChanged(key, kind, this);
        }
        render() {
            const { project } = this;
            if (!project) {
                return html ``;
            }
            if (!this.hasItems) {
                return this._emptyStateTemplate();
            }
            const content = this._renderParentChildrenTemplate(project);
            return html ` ${this.renderLabel()} ${this.renderOuterList(content)} `;
        }
        _renderParentChildrenTemplate(parent) {
            const { key } = parent;
            const folders = parent.listFolders();
            const requests = parent.listRequests();
            const environments = parent.listEnvironments();
            const isProject = parent.getProject() === undefined;
            const isEmpty = !folders.length && !environments.length && !requests.length;
            if (isEmpty) {
                return html `<p class="list-item-content empty" aria-disabled="true">Empty folder</p>`;
            }
            return html `
      ${folders.map((f) => this.renderFolder(f, isProject ? undefined : key))}
      ${environments.map((r) => this.renderEnvironment(r, isProject ? undefined : key))}
      ${requests.map((r) => this.renderRequest(r, isProject ? undefined : key))}
    `;
        }
        _emptyStateTemplate() {
            return html `
      ${this.renderLabel()}
      <div class="empty-state">
        <p>This project has no items.</p>
        <p>Right-click on this panel to add a request.</p>
      </div>
    `;
        }
        renderFolder(folder, parentKey) {
            const content = this._renderParentChildrenTemplate(folder);
            const name = folder.info.name || 'Unnamed folder';
            const { kind, key } = folder;
            return this.renderParentListItem(key, kind, name, content, {
                parent: parentKey,
                parentIcon: 'folderFilled',
            });
        }
        renderRequest(request, parentKey) {
            const name = request.info.name || 'Unnamed request';
            const { key, kind } = request;
            const content = this.renderItemContent('request', name);
            return this.renderListItem(key, kind, name, content, {
                parent: parentKey,
                draggable: true,
            });
        }
        renderEnvironment(environment, parentKey) {
            const name = environment.info.name || 'Unnamed environment';
            const { key, kind } = environment;
            const content = this.renderItemContent('environment', name);
            return this.renderListItem(key, kind, name, content, {
                parent: parentKey,
                draggable: true,
            });
        }
    };
})();
export default ProjectNavigation;
//# sourceMappingURL=ProjectNavigation.js.map