"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* @flow */
const react_1 = __importStar(require("react"));
const prop_types_1 = __importDefault(require("prop-types"));
const classnames_1 = __importDefault(require("classnames"));
const idx_1 = __importDefault(require("idx"));
const get_1 = __importDefault(require("lodash/get"));
const moment_1 = __importDefault(require("moment"));
const withStyles_1 = __importDefault(require("components/hoc/withStyles"));
const types_1 = require("components/config/types");
const helpers_1 = require("components/helpers");
const styles_1 = __importDefault(require("./styles"));
class TimesheetRowDisplay extends react_1.Component {
    constructor() {
        super(...arguments);
        this.state = {
            dates: [],
        };
        this._calculateRangeOfDates = (startWeekDay, endWeekDay) => {
            const dates = helpers_1.getRangeDates(startWeekDay, endWeekDay);
            this.setState({ dates });
        };
        this._getDayHour = date => {
            const { timeSheetEntries } = this.props;
            let item = timeSheetEntries.data.filter(entry => moment_1.default(entry.date).isSame(date)) ||
                [];
            item = item[0] || {};
            return parseFloat(item.duration || 0);
        };
        this._getTotalHours = () => {
            const { timeSheetEntries } = this.props;
            let sum = 0;
            timeSheetEntries.data.map(entry => {
                sum += parseFloat(entry.duration || 0);
            });
            return sum;
        };
    }
    componentDidMount() {
        const { startWeekDay, endWeekDay } = this.props;
        if (startWeekDay && endWeekDay) {
            this._calculateRangeOfDates(startWeekDay, endWeekDay);
        }
    }
    componentDidUpdate(prevProps) {
        if (this.props.startWeekDay !== prevProps.startWeekDay ||
            this.props.endWeekDay !== prevProps.endWeekDay) {
            this._calculateRangeOfDates(this.props.startWeekDay, this.props.endWeekDay);
        }
    }
    _renderInputWrapper() {
        const { classes, hideDaysLabel, service } = this.props;
        const { dates } = this.state;
        let displayUnit = 'h';
        if (get_1.default(service, 'data.timeBasedType') === 'DAY') {
            displayUnit = 'd';
        }
        return (react_1.default.createElement("div", { className: classes.inputWrapper },
            dates.map((date, index) => (react_1.default.createElement("div", { className: classes.dayWrapper, key: index },
                !hideDaysLabel && (react_1.default.createElement("div", { className: classes.dayInitial }, moment_1.default(date).format('dd'))),
                !hideDaysLabel && (react_1.default.createElement("div", { className: classes.dayNumber }, moment_1.default(date).format('DD'))),
                react_1.default.createElement("div", { className: classes.dayInput },
                    react_1.default.createElement("div", { className: classnames_1.default(classes.textValueBox, classes.dayInputWapper) }, this._getDayHour(date)))))),
            react_1.default.createElement("div", { className: classes.dayWrapper },
                !hideDaysLabel && react_1.default.createElement("div", { className: classes.totalLabel }, "Total"),
                react_1.default.createElement("div", { className: classes.totalHoursWrapper },
                    react_1.default.createElement("div", { className: classes.totalHours }, this._getTotalHours()),
                    react_1.default.createElement("div", { className: classes.hoursSymbol }, displayUnit)))));
    }
    render() {
        const { classes, project, customer, service } = this.props;
        const isProjectExist = get_1.default(project, 'data');
        return (react_1.default.createElement("div", { className: classes.wrapper },
            react_1.default.createElement("div", { className: classes.selectionWrapper },
                react_1.default.createElement("div", { className: classnames_1.default(classes.selectBtn, classes.projectBtn, {
                        active: isProjectExist,
                        disabled: !isProjectExist,
                    }) },
                    react_1.default.createElement("i", { className: "icon icon-projects" })),
                react_1.default.createElement("div", { className: classnames_1.default(classes.selectBtn, classes.userBtn, {
                        active: !isProjectExist,
                        disabled: isProjectExist,
                    }) },
                    react_1.default.createElement("i", { className: "icon icon-user-account" })),
                isProjectExist ? (react_1.default.createElement("div", { className: classnames_1.default(classes.textValueBox, classes.pDropdown) }, idx_1.default(project, _ => _.data.name))) : (react_1.default.createElement("div", { className: classnames_1.default(classes.textValueBox, classes.pDropdown) }, idx_1.default(customer, _ => _.data.name))),
                react_1.default.createElement("div", { className: classnames_1.default(classes.textValueBox, classes.sDropdown) }, idx_1.default(service, _ => _.data.name))),
            this._renderInputWrapper()));
    }
}
TimesheetRowDisplay.propTypes = Object.assign({ startWeekDay: prop_types_1.default.any, endWeekDay: prop_types_1.default.any, hideDaysLabel: prop_types_1.default.bool, project: prop_types_1.default.object, customer: prop_types_1.default.object, service: prop_types_1.default.object, timeSheetEntries: prop_types_1.default.object }, types_1.withStylesProps);
TimesheetRowDisplay.defaultProps = {
    onRemoveClick: () => { },
    hideRemoveIcon: false,
    hideDaysLabel: false,
};
exports.default = withStyles_1.default(styles_1.default)(TimesheetRowDisplay);
//# sourceMappingURL=index.js.map