<?php

/** @var \App\Models\Pro $hcpPro */
/** @var \App\Models\Pro $pro */
/** @var \App\Models\Note $note */

if(!@$hcpPro) {
    if(@$note) {
        $hcpPro = $note->hcpPro;
    }
    else {
        $hcpPro = $pro; // should never get here
    }
}


$canvasData = [];
if ($patient->canvas_data) {
    $canvasData = json_decode($patient->canvas_data, true);

    if(!isset($canvasData["cc_hpi"])) $canvasData["cc_hpi"] = [];
    if(!isset($canvasData["cc_hpi"][$hcpPro->id])) {
        $canvasData["cc_hpi"][$hcpPro->id] = [];
    }

    // if next_fu->next_cc available, prefill with that
    if (isset($canvasData["next_fu"]) && isset($canvasData["next_fu"][$hcpPro->id])) {

        $nextFU = $canvasData["next_fu"][$hcpPro->id];
        if(isset($nextFU['next_cc']) && isset($nextFU['next_cc']['list']) && count($nextFU['next_cc']['list'])) {
            $prefillData = array_map(function($_x) {
                if(!isset($_x['icd'])) $_x['icd'] = '';
                if(!isset($_x['memo'])) $_x['memo'] = '';
                $_x['hpi'] = [
                    'free_text' => ''
                ];
                return $_x;
            }, $nextFU['next_cc']['list']);
            $canvasData["cc_hpi"][$hcpPro->id]['current_cch'] = [
                'list' => $prefillData
            ];
        }
    }
    $canvasData = $canvasData['cc_hpi'][$hcpPro->id];

}
$contentData = $canvasData;
