import { dateSeparator } from "@baloise/web-app-utils";
import { NUMBER_KEYS } from "../../constants/keys.constant";
import { MaskBlock } from "../blocks";
import { AbstractMask } from "../mask";
import { BalDate } from "../../date";
export class DateMask extends AbstractMask {
  constructor() {
    super([
      new MaskBlock({
        from: 0,
        to: 2,
        allowedKeys: [...NUMBER_KEYS],
        format: (value, _locale, mask) => {
          if (Number.isNaN(parseInt(value, 10))) {
            return `${mask}${mask}`;
          }
          return value.padStart(2, '0');
        },
        mask: locale => {
          return this.dayMask[locale.split('-')[0]] || this.dayMask['de'];
        },
      }),
      new MaskBlock({ from: 2, to: 3, mask: locale => dateSeparator(locale), isSeparator: true }),
      new MaskBlock({
        from: 3,
        to: 5,
        allowedKeys: [...NUMBER_KEYS],
        format: (value, _locale, mask) => {
          if (Number.isNaN(parseInt(value, 10))) {
            return `${mask}${mask}`;
          }
          return value.padStart(2, '0');
        },
        mask: locale => this.monthMask[locale.split('-')[0]] || this.monthMask['de'],
      }),
      new MaskBlock({ from: 5, to: 6, mask: locale => dateSeparator(locale), isSeparator: true }),
      new MaskBlock({
        from: 6,
        to: 10,
        allowedKeys: [...NUMBER_KEYS],
        mask: locale => this.yearMask[locale.split('-')[0]] || this.yearMask['de'],
      }),
    ]);
    this.maxLength = 10;
    this.minLength = 10;
    this.dayMask = {
      de: 'T',
      en: 'D',
      fr: 'J',
      it: 'G',
      nl: 'D',
      es: 'D',
      pl: 'D',
      pt: 'D',
      sv: 'D',
      fi: 'M',
    };
    this.monthMask = {
      de: 'M',
      en: 'M',
      fr: 'M',
      it: 'M',
      nl: 'M',
      es: 'M',
      pl: 'M',
      pt: 'M',
      sv: 'M',
      fi: 'K',
    };
    this.yearMask = {
      de: 'J',
      en: 'Y',
      fr: 'A',
      it: 'A',
      nl: 'J',
      es: 'A',
      pl: 'R',
      pt: 'A',
      sv: 'Å',
      fi: 'V',
    };
  }
  onParseValue(inputValue) {
    if (inputValue) {
      const date = BalDate.fromAnyFormat(this.blocks.getRawValueWithoutMask(inputValue));
      if (date.isValid) {
        return date.toISODate();
      }
    }
    return '';
  }
  onFormatValue(isoDate) {
    if (isoDate) {
      const date = BalDate.fromISO(isoDate);
      if (date.isValid) {
        return date.toFormat();
      }
    }
    return '';
  }
  onPaste(context, _block, _index) {
    const value = context.clipboardData || '';
    const date = BalDate.fromAnyFormat(value);
    if (date.isValid) {
      context.value = date.toFormat();
      context.position.toEnd();
    }
  }
  onBlur(context) {
    const rawValue = this.blocks.getRawValueWithoutMaskByContext(context);
    const date = BalDate.fromAnyFormat(rawValue);
    if (date.isValid) {
      const formattedDate = date.toFormat();
      if (formattedDate !== context.value) {
        context.value = formattedDate;
      }
    }
  }
}
