{"size": 2907, "ext": "js", "max_stars_repo_path": "src/moduler/dialog/dialog-selector.js", "max_stars_repo_name": "jan-berge-ommedal/aktivitetsplan", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/moduler/dialog/dialog-selector.js", "max_issues_repo_name": "jan-berge-ommedal/aktivitetsplan", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/moduler/dialog/dialog-selector.js", "max_forks_repo_name": "jan-berge-ommedal/aktivitetsplan", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.6857142857, "max_line_length": 92, "alphanum_fraction": 0.690746474}
import { createSelector } from 'reselect';
import { moment } from '../../utils';
import { newDatoErIPeriode } from '../filtrering/filter/filter-utils';
import { selectHistoriskPeriode } from '../filtrering/filter/filter-selector';
import { selectForrigeHistoriskeSluttDato } from '../oppfolging-status/oppfolging-selector';
import { erEskaleringsDialog } from './dialog-utils';
import {
    selectErBruker,
    selectErVeileder,
} from '../identitet/identitet-selector';
import { STATUS } from '../../ducks/utils';

function selectDialogSlice(state) {
    return state.data.dialog;
}

function selectDialogViewSlice(state) {
    return state.view.dialog;
}

export function selectDialogStatus(state) {
    return selectDialogSlice(state).status;
}

export function selectEskaleringsFilter(state) {
    return selectDialogSlice(state).esklaringsFilter;
}

export function selectAlleDialoger(state) {
    return selectDialogSlice(state).data;
}

const hentDialogerFraState = (
    dialoger,
    esklaringsFilter,
    historiskPeriode,
    forrigeSluttDato
) =>
    dialoger.data
        .filter(d =>
            newDatoErIPeriode(
                d.opprettetDato,
                historiskPeriode,
                forrigeSluttDato
            )
        )
        .filter(d => erEskaleringsDialog(d) || !esklaringsFilter);

export const selectDialoger = createSelector(
    [
        selectDialogSlice,
        selectEskaleringsFilter,
        selectHistoriskPeriode,
        selectForrigeHistoriskeSluttDato,
    ],
    hentDialogerFraState
);

export function selectDialogMedId(state, dialogId) {
    return selectDialoger(state).find(d => d.id === dialogId);
}

export function selectDialogForAktivitetId(state, aktivitetId) {
    return selectAlleDialoger(state).find(d => d.aktivitetId === aktivitetId);
}

export function selectHarUbehandledeDialoger(state) {
    const data = selectDialoger(state);
    return (
        data.filter(
            dialog =>
                dialog.historisk === false &&
                (dialog.ferdigBehandlet === false || dialog.venterPaSvar)
        ).length > 0
    );
}

export function selectHarEskaleringer(state) {
    return selectDialoger(state).filter(erEskaleringsDialog).length > 0;
}

export function selectVisEskaleringsFilter(state) {
    return selectHarEskaleringer(state) && selectErBruker(state);
}

export function selectTilpasseDialogModalHistoriskVisning(state) {
    return (
        selectErVeileder(state) ||
        (!selectHarEskaleringer(state) && selectErBruker(state))
    );
}

export function selectVisBrukerInfo(state, dialogId_) {
    const { dialogId, utlopTidspunkt } = selectDialogViewSlice(state).data;
    return (
        !!utlopTidspunkt &&
        utlopTidspunkt.isAfter(moment()) &&
        dialogId === dialogId_
    );
}

export function selectHarTilgangTilDialog(state) {
    return selectDialogStatus(state) === STATUS.OK;
}
