<?php

$result = sql_fetch_assoc(
    sql_select_field($config['table_prefix']. "settings", '*', 'name="cbext.clientbase.wazzup"')
);

if (
    isset($result['value']) && !empty($result['value'])
) {
    $settings = \Symfony\Component\Yaml\Yaml::parse($result['value']);

    if (
        isset($settings['table']) &&
        isset($settings['table']['id']) &&
        !isset($settings['calc_id'])
    ) {
        $settings['calc_id'] = sql_insert($config['table_prefix'] ."calc", [
            'table_id' => $settings['table']['id'],
            'name' => 'wazzup calculation',
            'calculate' => '\CB\Apiloc::query("POST", "/api/dev/cbext/clientbase/wazzup/send", [], $line);',
            'link_fields' => 'a:0:{}',
            'recursion_disabled' => '1',
            'construct_params' => '',
            'start_calc' => '0',
            'help' => 'Системное вычисление расширения wazzup.',
        ]);

        sql_insert($config['table_prefix'] ."calc_cond", [
            'calc_id' => $settings['calc_id'],
            'type' => '3',
            'param' => '-1',
            'param2' => '0',
        ]);

        sql_update($config['table_prefix'] ."settings", [
            'value' => \Symfony\Component\Yaml\Yaml::dump($settings, 8, 2)
        ], "name='cbext.clientbase.wazzup'");
    }
}
