{% macro merge_dict(current_dict, to_merge_dict, dict_key='name') %}
{%   set merged_dict = current_dict %}
{%   if to_merge_dict %}
{%     if to_merge_dict is mapping %}
{%       for dict_name in to_merge_dict.keys() | sort %}
{%         if to_merge_dict[dict_name][dict_key]|d() %}
{%           set _ = merged_dict.update({to_merge_dict[dict_name][dict_key]:(current_dict[to_merge_dict[dict_name][dict_key]]|d({}) | combine(to_merge_dict[dict_name], recursive=True))}) %}
{%         elif to_merge_dict[dict_name][dict_key] is undefined %}
{%           set _ = merged_dict.update({dict_name:(current_dict[dict_name]|d({}) | combine(to_merge_dict[dict_name], recursive=True))}) %}
{%         endif %}
{%       endfor %}
{%     elif to_merge_dict is not string and to_merge_dict is not mapping %}
{%       set flattened_dict = lookup("flattened", to_merge_dict) %}
{%       for element in ([ flattened_dict ] if flattened_dict is mapping else flattened_dict) %}
{%         if element[dict_key]|d() %}
{%           set _ = merged_dict.update({element[dict_key]:(current_dict[element[dict_key]]|d({}) | combine(element, recursive=True))}) %}
{%         endif %}
{%       endfor %}
{%     endif %}
{%   endif %}
{{ merged_dict | to_json }}
{% endmacro %}
{% set cron__tpl_merge_default =   (merge_dict({},                        cron__default_jobs,   'file') | from_json) %}
{% set cron__tpl_merge_dependent = (merge_dict(cron__tpl_merge_default,   cron__dependent_jobs, 'file') | from_json) %}
{% set cron__tpl_merge_all =       (merge_dict(cron__tpl_merge_dependent, cron__jobs,           'file') | from_json) %}
{% set cron__tpl_merge_group =     (merge_dict(cron__tpl_merge_all,       cron__group_jobs,     'file') | from_json) %}
{% set cron__tpl_items =           (merge_dict(cron__tpl_merge_group,     cron__host_jobs,      'file') | from_json) %}
{% set cron__tpl_filtered_items = [] %}
{% for item_name, params in cron__tpl_items.items() %}
{%   set cron_file = (params["file"] | d(item_name)) %}
{%   if params["cron_file"] is undefined %}
{%     set _ = params.update({"cron_file":cron_file}) %}
{%   endif %}
{%   if params["name"] is undefined %}
{%     set _ = params.update({"name":"Autogenerated name for " + cron_file}) %}
{%   endif %}
{%   if params["jobs"] is undefined %}
{%     set _ = params.update({"jobs":[]}) %}
{%     set cron__tpl_job = {} %}
{%     for parameter in params.keys() | sort %}
{%       if parameter not in [ 'file', 'cron_file', 'backup', 'environment', 'user', 'jobs', 'custom_files' ] %}
{%         set _ = cron__tpl_job.update({parameter:params[parameter]}) %}
{%       endif %}
{%     endfor %}
{%     set _ = params["jobs"].append(cron__tpl_job) %}
{%   elif params["jobs"] %}
{%     set cron__tpl_filtered_jobs = [] %}
{%     for job in params["jobs"] %}
{%       set cron__tpl_job = {} %}
{%       if job is string %}
{%         set _ = cron__tpl_job.update({"job":job}) %}
{%       elif job is mapping %}
{%         for parameter in job.keys() | sort %}
{%           if parameter not in [ 'file', 'cron_file', 'backup', 'environment', 'user', 'jobs', 'custom_files' ] %}
{%             set _ = cron__tpl_job.update({parameter:job[parameter]}) %}
{%           endif %}
{%         endfor %}
{%       endif %}
{%       if cron__tpl_job["name"] is undefined %}
{%         set _ = cron__tpl_job.update({"name":"Autogenerated name for " + cron_file + ", job #" + loop.index|string}) %}
{%       endif %}
{%       for parameter in params.keys() | sort %}
{%         if parameter not in [ 'file', 'cron_file', 'backup', 'environment', 'user', 'jobs', 'name', 'job', 'custom_files' ] %}
{%           set _ = cron__tpl_job.update({parameter:params[parameter]}) %}
{%         endif %}
{%       endfor %}
{%       set _ = cron__tpl_filtered_jobs.append(cron__tpl_job) %}
{%     endfor %}
{%     set _ = params.update({"jobs":cron__tpl_filtered_jobs}) %}
{%   endif %}
{%   if params["environment"]|d() %}
{%     if params["environment"] is mapping %}
{%       set cron__tpl_filtered_environment = [] %}
{%       for env_key, env_value in params["environment"].items() %}
{%         set _ = cron__tpl_filtered_environment.append({env_key:env_value}) %}
{%       endfor %}
{%       set _ = params.update({"environment":cron__tpl_filtered_environment}) %}
{%     endif %}
{%   endif %}
{%   set _ = cron__tpl_filtered_items.append(params) %}
{% endfor %}
{{ cron__tpl_filtered_items | to_yaml }}
