// #region Imports
import { AsyncDirective, PartType, directive } from 'lit/async-directive.js';
// #endregion
/**
 * The `InterpolateDirective` class.
 *
 * @private
 */
class InterpolateDirective extends AsyncDirective {
    constructor(partInfo) {
        super(partInfo);
        if (partInfo.type !== PartType.PROPERTY) {
            throw new Error('The interpolate directive can only be used in property bindings.');
        }
    }
    update(_part, [key, ...args]) {
        return this.render(key, ...args);
    }
    render(key, ...args) {
        const value = this.format(key, ...args);
        this.setValue(value);
        return value;
    }
    /**
     * Formats the given string with the given arguments.
     *
     * @private
     */
    format(self, ...args) {
        const allArgs = new Array();
        args.forEach((x) => {
            if (Array.isArray(x)) {
                x.forEach((y) => {
                    allArgs.push(y);
                });
            }
            else {
                allArgs.push(x);
            }
        });
        return self.replace(/{(\d+)}/g, (match, index) => {
            if (allArgs[index] !== undefined) {
                return allArgs[index];
            }
            return match;
        });
    }
}
/**
 * @public
 */
export const interpolate = directive(InterpolateDirective);
//# sourceMappingURL=InterpolateDirective.js.map