import { MaskPosition } from "./mask-position";
export class MaskContext {
  constructor(_options) {
    this._options = _options;
    this._value = '';
    if (this._options.component && this._options.component.nativeInput) {
      this._value = this._options.component.nativeInput.value;
    }
    this.position = new MaskPosition(this._options);
  }
  get target() {
    if (this._options.component && this._options.component.nativeInput) {
      return this._options.component.nativeInput;
    }
    return undefined;
  }
  get value() {
    return this._value;
  }
  set value(newValue) {
    this._value = newValue;
  }
  get focused() {
    return this._options.component.focused;
  }
  isValueEmpty() {
    return this.value === '' || this.value === undefined || this.value === null;
  }
  preventDefault() {
    if (this._options.event.preventDefault) {
      this._options.event.preventDefault();
    }
  }
  stopPropagation() {
    this.preventDefault();
    if (this._options.event.stopPropagation) {
      this._options.event.stopPropagation();
    }
  }
  async submit(eventType = 'input', parsedValue) {
    if (this.target) {
      this.target.value = this.value;
      if (eventType !== 'blur' && eventType !== 'tab') {
        this.position.submit();
      }
      if (this._options.component) {
        if (eventType === 'input' || eventType === 'tab') {
          this._options.component.balInput.emit(this.value);
        }
        if ((eventType === 'change' || eventType === 'blur') && parsedValue !== undefined) {
          const valueChanged = this._options.component.value !== parsedValue;
          if (valueChanged) {
            this._options.component.value = parsedValue;
            this._options.component.balChange.emit(parsedValue);
          }
        }
      }
    }
  }
}
