<commit_before>'use strict';

// stdlib
import React from 'react';
import createReactClass from 'create-react-class';
import PropTypes from 'prop-types';

// third party lib
import _ from 'underscore';
import { ContextualHelp } from '../../../../libs/bootstrap/contextual_help';
import moment from 'moment';

// Internal lib
import { EvidenceModalManager } from './evidenceModalManager';
import { DeleteEvidenceModal} from './deleteEvidenceModal';
import { extraEvidence } from './segregationData';
import { external_url_map } from '../../../globals';
import { PmidSummary } from '../../../curator';
import { getAffiliationName } from '../../../../libs/get_affiliation_name';

let EvidenceTable = createReactClass({
    propTypes: {
        tableData: PropTypes.array,                 // Evidence data for this table
        allData: PropTypes.array,                   // All extra evidence we've collected
        subcategory: PropTypes.string,              // subcategory (usually the panel) the evidence is part of
        deleteEvidenceFunc: PropTypes.func,         // Function to call to delete an evidence
        evidenceCollectionDone: PropTypes.func,     // Function to call to add or edit an evidence
        criteriaList: PropTypes.array,              // ACMG criteria
        session: PropTypes.object,                  // Session object
        affiliation: PropTypes.object,              // User's affiliation
        viewOnly: PropTypes.bool,                   // If the page is in read-only mode
        canCurrUserModifyEvidence: PropTypes.func   // Function to check if current logged in user can modify the given evidence
    },

    getInitialState() {
        let tableFormat = _.find(extraEvidence.tableCols(), (table) => table.subcategory == this.props.subcategory);

        return {
            tableFormat: tableFormat,
            rows: this.props.tableData,
        };
    },

    /**
     * Check if current user can edit/delete the given evidence
     *
     * @param {object} row   The evidence row
     */
    canModify(row) {
        if (this.props.viewOnly === true) {
            return false;
        }
        return this.props.canCurrUserModifyEvidence(row);
    },

    /**
     * Return the criteria codes that are mapped with given column
     * 
     * @param {string} colKey  The column key
     */
    getCriteriaCodes(colKey) {
        let criteriaCodes = extraEvidence.fieldToCriteriaCodeMapping.filter(o => o.key === colKey);
        return criteriaCodes;
    },

    /**
     * Return the number of criteria that has value in this evidence
     * 
     * @param {object} source  The evidence source
     */
    getSubRowCount(source) {
        let count = 0;
        // The criteria under Specificity of phenotype panel has only one corresponding comment so display them on same row.
        // SubRowCount is 1.
        if (this.props.subcategory === 'specificity-of-phenotype') {
            count = 1;
        } else {
            if (source['relevant_criteria']) {
                let relevantData = _.find(extraEvidence.sheetToTableMapping, o => o.subcategory === this.props.subcategory);
                let cols = relevantData.cols.map(o => o.key);
                cols.forEach(col => {
                    const colComment = col + '_comment';
                    if ((source[col] && source[col] != '') || (source[colComment] && source[colComment] != '')) {
                        count++;
                    }
                });
            }
        }
        return count;
    },

    /**
     * Return the formatted source title for given evidence
     * 
     * @param {object} row  The evidence row data 
     */
    getSourceColumnContent(row) {
        let nodeContent = null;
        let metadata = row.sourceInfo.metadata;

        if (metadata['_kind_key'] === 'PMID') {
            if (row.articles.length > 0) {
                nodeContent = <PmidSummary
                    article = {row.articles[0]}
                    pmidLinkout
                />
            } else if (metadata.pmid) { 
                nodeContent = <a
                    href = {external_url_map['PubMed'] + metadata.pmid}
                    target = "_blank"
                    title = {`PubMed Article ID: ${metadata.pmid}`}
                >
                    PMID {pmid}
                </a>
            }
        } else {
            let content = null;
            let help = null;
            const separator = ', ';
            switch (metadata['_kind_key']) {
                case 'clinical_lab':
                    content = metadata.lab_name;
                    help = (metadata.clinvar_gtr_labid ? `Clinvar/GTR LabID: ${metadata.clinvar_gtr_labid}` : '') +
                           (metadata.clinvar_gtr_labid && metadata.clinvar_scv ? `${separator}` : '') +
                           (metadata.clinvar_scv ? `ClinVar Submission Accession (SCV): ${metadata.clinvar_scv}` : '');
                    break;
                case 'clinic':
                    content = metadata.institutional_affiliation;
                    help = (metadata.department_affiliation ? `Department Affiliation: ${metadata.department_affiliation}` : '') +
                           (metadata.department_affiliation && metadata.clinvar_gtr_labid ? `${separator}` : '') +
                           (metadata.clinvar_gtr_labid ? `Clinvar/GTR LabID: ${metadata.clinvar_gtr_labid}` : '') +
                           ((metadata.department_affiliation || metadata.clinvar_gtr_labid) && metadata.clinvar_scv ? `${separator}` : '') +
                           (metadata.clinvar_scv ? ` ClinVar Submission Accession (SCV): ${metadata.clinvar_scv}` : '');
                    break;
                case 'research_lab':
                    content = metadata.institutional_affiliation;
                    help = (metadata.department_affiliation ? `Department Affiliation/Principal Investigator: ${metadata.department_affiliation}` : '') +
                           (metadata.department_affiliation && metadata.clinvar_gtr_labid ? `${separator}` : '') +
                           (metadata.clinvar_gtr_labid ? ` Clinvar/GTR LabID: ${metadata.clinvar_gtr_labid}` : '') +
                           ((metadata.department_affiliation || metadata.clinvar_gtr_labid) && metadata.clinvar_scv ? `${separator}` : '') +
                           (metadata.clinvar_scv ? ` ClinVar Submission Accession (SCV): ${metadata.clinvar_scv}` : '');
                    break;
                case 'public_database':
                    content = metadata.name;
                    help = (metadata.url ? `URL: ${metadata.url}` : '') +
                           (metadata.url && metadata.variant_id ? `${separator}` : '') +
                           (metadata.variant_id ? ` Variant ID: ${metadata.variant_id}` : '') +
                           ((metadata.url || metadata.variant_id) && metadata.clinvar_gtr_labid ? `${separator}` : '') +
                           (metadata.clinvar_gtr_labid ? ` Clinvar/GTR LabID: ${metadata.clinvar_gtr_labid}` : '') +
                           ((metadata.url || metadata.variant_id || metadata.clinvar_gtr_labid) && metadata.clinvar_scv ? `${separator}` : '') +
                           (metadata.clinvar_scv ? ` ClinVar Submission Accession (SCV): ${metadata.clinvar_scv}` : '');
                    break;
                default:
                    content = Object.keys(metadata)
                        .filter(k => !k.startsWith('_'))
                        .map(k => metadata[k])
                        .join(', ');
                    break;
            }

            nodeContent = help && help.length
                ? <span>
                  {content} <ContextualHelp content={help}></ContextualHelp>
                  </span>
                : <span>{content}</span>;
        }

        return nodeContent;
    },

    /**
     * Return the edit evidence button for this row
     * 
     * @param {object} row       Evidence in this row
     */
    getEditButton(row) {
        return (
            <EvidenceModalManager
                data = {row}
                allData = {this.props.allData}<commit_msg>Changed to use variable directly.<commit_after>'use strict';

// stdlib
import React from 'react';
import createReactClass from 'create-react-class';
import PropTypes from 'prop-types';

// third party lib
import _ from 'underscore';
import { ContextualHelp } from '../../../../libs/bootstrap/contextual_help';
import moment from 'moment';

// Internal lib
import { EvidenceModalManager } from './evidenceModalManager';
import { DeleteEvidenceModal} from './deleteEvidenceModal';
import { extraEvidence } from './segregationData';
import { external_url_map } from '../../../globals';
import { PmidSummary } from '../../../curator';
import { getAffiliationName } from '../../../../libs/get_affiliation_name';

let EvidenceTable = createReactClass({
    propTypes: {
        tableData: PropTypes.array,                 // Evidence data for this table
        allData: PropTypes.array,                   // All extra evidence we've collected
        subcategory: PropTypes.string,              // subcategory (usually the panel) the evidence is part of
        deleteEvidenceFunc: PropTypes.func,         // Function to call to delete an evidence
        evidenceCollectionDone: PropTypes.func,     // Function to call to add or edit an evidence
        criteriaList: PropTypes.array,              // ACMG criteria
        session: PropTypes.object,                  // Session object
        affiliation: PropTypes.object,              // User's affiliation
        viewOnly: PropTypes.bool,                   // If the page is in read-only mode
        canCurrUserModifyEvidence: PropTypes.func   // Function to check if current logged in user can modify the given evidence
    },

    getInitialState() {
        let tableFormat = _.find(extraEvidence.tableCols(), (table) => table.subcategory == this.props.subcategory);

        return {
            tableFormat: tableFormat,
            rows: this.props.tableData,
        };
    },

    /**
     * Check if current user can edit/delete the given evidence
     *
     * @param {object} row   The evidence row
     */
    canModify(row) {
        if (this.props.viewOnly === true) {
            return false;
        }
        return this.props.canCurrUserModifyEvidence(row);
    },

    /**
     * Return the criteria codes that are mapped with given column
     * 
     * @param {string} colKey  The column key
     */
    getCriteriaCodes(colKey) {
        let criteriaCodes = extraEvidence.fieldToCriteriaCodeMapping.filter(o => o.key === colKey);
        return criteriaCodes;
    },

    /**
     * Return the number of criteria that has value in this evidence
     * 
     * @param {object} source  The evidence source
     */
    getSubRowCount(source) {
        let count = 0;
        // The criteria under Specificity of phenotype panel has only one corresponding comment so display them on same row.
        // SubRowCount is 1.
        if (this.props.subcategory === 'specificity-of-phenotype') {
            count = 1;
        } else {
            if (source['relevant_criteria']) {
                let relevantData = _.find(extraEvidence.sheetToTableMapping, o => o.subcategory === this.props.subcategory);
                let cols = relevantData.cols.map(o => o.key);
                cols.forEach(col => {
                    const colComment = col + '_comment';
                    if ((source[col] && source[col] != '') || (source[colComment] && source[colComment] != '')) {
                        count++;
                    }
                });
            }
        }
        return count;
    },

    /**
     * Return the formatted source title for given evidence
     * 
     * @param {object} row  The evidence row data 
     */
    getSourceColumnContent(row) {
        let nodeContent = null;
        let metadata = row.sourceInfo.metadata;

        if (metadata['_kind_key'] === 'PMID') {
            if (row.articles.length > 0) {
                nodeContent = <PmidSummary
                    article = {row.articles[0]}
                    pmidLinkout
                />
            } else if (metadata.pmid) { 
                nodeContent = <a
                    href = {external_url_map['PubMed'] + metadata.pmid}
                    target = "_blank"
                    title = {`PubMed Article ID: ${metadata.pmid}`}
                >
                    PMID {pmid}
                </a>
            }
        } else {
            let content = null;
            let help = null;
            const separator = ', ';
            switch (metadata['_kind_key']) {
                case 'clinical_lab':
                    content = metadata.lab_name;
                    help = (metadata.clinvar_gtr_labid ? `Clinvar/GTR LabID: ${metadata.clinvar_gtr_labid}` : '') +
                           (metadata.clinvar_gtr_labid && metadata.clinvar_scv ? separator : '') +
                           (metadata.clinvar_scv ? `ClinVar Submission Accession (SCV): ${metadata.clinvar_scv}` : '');
                    break;
                case 'clinic':
                    content = metadata.institutional_affiliation;
                    help = (metadata.department_affiliation ? `Department Affiliation: ${metadata.department_affiliation}` : '') +
                           (metadata.department_affiliation && metadata.clinvar_gtr_labid ? separator : '') +
                           (metadata.clinvar_gtr_labid ? `Clinvar/GTR LabID: ${metadata.clinvar_gtr_labid}` : '') +
                           ((metadata.department_affiliation || metadata.clinvar_gtr_labid) && metadata.clinvar_scv ? separator : '') +
                           (metadata.clinvar_scv ? ` ClinVar Submission Accession (SCV): ${metadata.clinvar_scv}` : '');
                    break;
                case 'research_lab':
                    content = metadata.institutional_affiliation;
                    help = (metadata.department_affiliation ? `Department Affiliation/Principal Investigator: ${metadata.department_affiliation}` : '') +
                           (metadata.department_affiliation && metadata.clinvar_gtr_labid ? separator : '') +
                           (metadata.clinvar_gtr_labid ? ` Clinvar/GTR LabID: ${metadata.clinvar_gtr_labid}` : '') +
                           ((metadata.department_affiliation || metadata.clinvar_gtr_labid) && metadata.clinvar_scv ? separator : '') +
                           (metadata.clinvar_scv ? ` ClinVar Submission Accession (SCV): ${metadata.clinvar_scv}` : '');
                    break;
                case 'public_database':
                    content = metadata.name;
                    help = (metadata.url ? `URL: ${metadata.url}` : '') +
                           (metadata.url && metadata.variant_id ? separator : '') +
                           (metadata.variant_id ? ` Variant ID: ${metadata.variant_id}` : '') +
                           ((metadata.url || metadata.variant_id) && metadata.clinvar_gtr_labid ? separator : '') +
                           (metadata.clinvar_gtr_labid ? ` Clinvar/GTR LabID: ${metadata.clinvar_gtr_labid}` : '') +
                           ((metadata.url || metadata.variant_id || metadata.clinvar_gtr_labid) && metadata.clinvar_scv ? separator : '') +
                           (metadata.clinvar_scv ? ` ClinVar Submission Accession (SCV): ${metadata.clinvar_scv}` : '');
                    break;
                default:
                    content = Object.keys(metadata)
                        .filter(k => !k.startsWith('_'))
                        .map(k => metadata[k])
                        .join(', ');
                    break;
            }

            nodeContent = help && help.length
                ? <span>
                  {content} <ContextualHelp content={help}></ContextualHelp>
                  </span>
                : <span>{content}</span>;
        }

        return nodeContent;
    },

    /**
     * Return the edit evidence button for this row
     * 
     * @param {object} row       Evidence in this row
     */
    getEditButton(row) {
        return (
            <EvidenceModalManager
                data = {row}
                allData = {this.props.allData}
{'fraction_non_alphanumeric': 0.1145947928126146, 'fraction_numerical': 0.0006111722283339445, 'mean_word_length': 1.6992741669416034, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3233331', 'n_tokens_mistral': 4403, 'n_tokens_neox': 4159, 'n_words': 973}