import moment from 'moment';
import { noView } from 'aurelia-framework';

@noView
export class DateFormatValueConverter {
    toView(value, format) {
        console.debug('toview ' + value);
        if (!value) {
          console.debug('toview return null');
          return null;
        }
        let m = moment(value);
        
        if (m.isValid()) {          
          console.debug('toview return formatted date');
            return m.format(format);
        }
        if (!m.isValid()) {
          console.debug('toview not valid date return value');
          return value;
        }
        if (m.year() <= 1970) {
          console.debug('toview year <= 1970 return empty');
           return "";
        }
        console.debug('toview return value');
        return value;
    }
    fromView(value) {
      let m = moment(value, 'DD/MM/YYYY', true);
      if (m.isValid()) {
        console.debug('input correctly parsed as date', m.toDate());
        return m.set("hour", 12).toDate();
      }
      console.debug('not a date', value);
      console.debug('return null');
      return null;
    }
}

