function _check_private_redeclaration(obj, privateCollection) {
    if (privateCollection.has(obj)) {
        throw new TypeError("Cannot initialize the same private elements twice on an object");
    }
}
function _class_apply_descriptor_get(receiver, descriptor) {
    if (descriptor.get) {
        return descriptor.get.call(receiver);
    }
    return descriptor.value;
}
function _class_apply_descriptor_set(receiver, descriptor, value) {
    if (descriptor.set) {
        descriptor.set.call(receiver, value);
    } else {
        if (!descriptor.writable) {
            throw new TypeError("attempted to set read only private field");
        }
        descriptor.value = value;
    }
}
function _class_extract_field_descriptor(receiver, privateMap, action) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to " + action + " private field on non-instance");
    }
    return privateMap.get(receiver);
}
function _class_private_field_get(receiver, privateMap) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
    return _class_apply_descriptor_get(receiver, descriptor);
}
function _class_private_field_init(obj, privateMap, value) {
    _check_private_redeclaration(obj, privateMap);
    privateMap.set(obj, value);
}
function _class_private_field_set(receiver, privateMap, value) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
    _class_apply_descriptor_set(receiver, descriptor, value);
    return value;
}
import { mixin } from '../../helpers/object.mjs';
import localHooks from '../../mixins/localHooks.mjs';
var /**
   * The field holds initial changes that will be used to notify the callbacks added using
   * subscribe method. Regardless of the moment of listening for changes, the subscriber
   * will be notified once with all changes made before subscribing.
   *
   * @type {Array}
   */ _currentInitialChanges = /*#__PURE__*/ new WeakMap();
/**
 * The ChangesObserver module is an object that represents a disposable resource
 * provided by the ChangesObservable module.
 *
 * @class ChangesObserver
 */ export class ChangesObserver {
    /**
   * Subscribes to the observer.
   *
   * @param {Function} callback A function that will be called when the new changes will appear.
   * @returns {ChangesObserver}
   */ subscribe(callback) {
        this.addLocalHook('change', callback);
        this._write(_class_private_field_get(this, _currentInitialChanges));
        return this;
    }
    /**
   * Unsubscribes all subscriptions. After the method call, the observer would not produce
   * any new events.
   *
   * @returns {ChangesObserver}
   */ unsubscribe() {
        this.runLocalHooks('unsubscribe');
        this.clearLocalHooks();
        return this;
    }
    /**
   * The write method is executed by the ChangesObservable module. The module produces all
   * changes events that are distributed further by the observer.
   *
   * @private
   * @param {object} changes The chunk of changes produced by the ChangesObservable module.
   * @returns {ChangesObserver}
   */ _write(changes) {
        if (changes.length > 0) {
            this.runLocalHooks('change', changes);
        }
        return this;
    }
    /**
   * The write method is executed by the ChangesObservable module. The module produces initial
   * changes that will be used to notify new subscribers.
   *
   * @private
   * @param {object} initialChanges The chunk of changes produced by the ChangesObservable module.
   */ _writeInitialChanges(initialChanges) {
        _class_private_field_set(this, _currentInitialChanges, initialChanges);
    }
    constructor(){
        _class_private_field_init(this, _currentInitialChanges, {
            writable: true,
            value: void 0
        });
        _class_private_field_set(this, _currentInitialChanges, []);
    }
}
mixin(ChangesObserver, localHooks);
