// #region Imports
import { directive, PartType } from 'lit/directive.js';
import { Task } from '@lit/task';
import { DirectiveBase } from '../Abstracts/DirectiveBase';
import { isReactiveControllerHost } from '../../Controllers/Utils';
class TaskDirective extends DirectiveBase {
    _task;
    constructor(args) {
        super(args);
        if (args.type !== PartType.CHILD) {
            throw new Error('The `task` directive can only be used in child expressions.');
        }
    }
    render(_promiseFn, renderer) {
        if (!this._task) {
            return typeof renderer === 'function'
                ? undefined
                : renderer.initial?.();
        }
        const r = typeof renderer === 'function'
            ? { complete: renderer }
            : renderer;
        return this._task.render(r);
    }
    update(part, [promiseFn, renderer]) {
        if (!part.options) {
            throw new Error('The `task` directive must be used in a host element.');
        }
        if (!part.options.host) {
            throw new Error('The `task` directive must be used in a host element.');
        }
        if (!isReactiveControllerHost(part.options.host)) {
            throw new Error('The `task` directive can only be used on reactive controller host elements.');
        }
        this._task ??= new Task(part.options.host, {
            task: (_args, opts) => promiseFn(opts),
            args: () => [],
            autoRun: true
        });
        return this.render(promiseFn, renderer);
    }
}
export const task = directive(TaskDirective);
//# sourceMappingURL=TaskDirective.js.map