import { Deferred as s } from "../utils/promise-utils.js";
import { e as a } from "../../../../../../../_virtual/events.js";
class f {
  constructor(t) {
    this._ready = new s(), this.settings = {}, this.workspaceConfig = !1, this.onConfigurationSectionUpdateEmitter = new a.Emitter(), this.serviceRegistry = t.ServiceRegistry;
  }
  get ready() {
    return this._ready.promise;
  }
  initialize(t) {
    var i, e;
    this.workspaceConfig = (e = (i = t.capabilities.workspace) === null || i === void 0 ? void 0 : i.configuration) !== null && e !== void 0 ? e : !1;
  }
  async initialized(t) {
    if (this.workspaceConfig) {
      if (t.register) {
        const i = this.serviceRegistry.all;
        t.register({
          // Listen to configuration changes for all languages
          section: i.map((e) => this.toSectionName(e.LanguageMetaData.languageId))
        });
      }
      if (t.fetchConfiguration) {
        const i = this.serviceRegistry.all.map((n) => ({
          // Fetch the configuration changes for all languages
          section: this.toSectionName(n.LanguageMetaData.languageId)
        })), e = await t.fetchConfiguration(i);
        i.forEach((n, o) => {
          this.updateSectionConfiguration(n.section, e[o]);
        });
      }
    }
    this._ready.resolve();
  }
  /**
   *  Updates the cached configurations using the `change` notification parameters.
   *
   * @param change The parameters of a change configuration notification.
   * `settings` property of the change object could be expressed as `Record<string, Record<string, any>>`
   */
  updateConfiguration(t) {
    t.settings && Object.keys(t.settings).forEach((i) => {
      const e = t.settings[i];
      this.updateSectionConfiguration(i, e), this.onConfigurationSectionUpdateEmitter.fire({ section: i, configuration: e });
    });
  }
  updateSectionConfiguration(t, i) {
    this.settings[t] = i;
  }
  /**
  * Returns a configuration value stored for the given language.
  *
  * @param language The language id
  * @param configuration Configuration name
  */
  async getConfiguration(t, i) {
    await this.ready;
    const e = this.toSectionName(t);
    if (this.settings[e])
      return this.settings[e][i];
  }
  toSectionName(t) {
    return `${t}`;
  }
  get onConfigurationSectionUpdate() {
    return this.onConfigurationSectionUpdateEmitter.event;
  }
}
export {
  f as DefaultConfigurationProvider
};
