import moment from 'moment';
import { observable, computed } from 'mobx';
import { isString, isEmpty, map, isNaN } from 'lodash';
import {
    identifiedBy,
} from 'mobx-decorated-models';

@identifiedBy('date-range')
export default class DateRange {

    @observable start;
    @observable end;

    constructor(range) {
        if (isString(range) && !isEmpty(range)) {
            [this.start, this.end] = map(range.split('...'), d => new Date(d));
        } else if (range) {
            this.start = range.start;
            this.end = range.end;
        }
    }

    toJSON() {
        if (this.start && this.end && !isNaN(this.start) && !isNaN(this.end)) {
            // this strange format is what PG user and is therefore what active record expects
            return `[${this.start.toISOString()},${this.end.toISOString()})`;
        }
        return '';
    }

    static serialize(range) {
        return (range ? range.toJSON() : '');
    }

    @computed get isCurrent() {
        return moment(this.end).isAfter() && moment(this.start).isBefore();
    }

}

@@ -632,16 +632,26 @@
 is.start
+.getTime()
 ) && !is
@@ -662,16 +662,26 @@
 this.end
+.getTime()
 )) %7B%0A   

test getTime for NaN