"""Public compatibility module.

Conceptually, this module preserves the broad historical helper surface used by
older notebooks and scripts.

It exists as a separate unit so a large stable API can remain available while
the implementation stays split across focused helper modules and optional
dependencies are imported lazily.

It connects the legacy all-in-one helper namespace to the current modular
`functions` package.
"""

from __future__ import annotations

import importlib
from typing import Any, Dict, Tuple

_SYMBOL_SOURCES: Dict[str, Tuple[str, str]] = {
    'FUNCTION_API_VERSION': ('constants', 'FUNCTION_API_VERSION'),
    'QuietTrialParams': ('behavior', 'QuietTrialParams'),
    'mad_nan': ('behavior', 'mad_nan'),
    'nwb_find_quiet_trial': ('behavior', 'nwb_find_quiet_trial'),
    'psth_behavior': ('behavior', 'psth_behavior'),
    'psth_simple': ('behavior', 'psth_simple'),
    'robust_mode': ('behavior', 'robust_mode'),
    'trial_type_maker': ('behavior', 'trial_type_maker'),
    'allen_ccf_bregma': ('brain_plotting', 'allen_ccf_bregma'),
    'brain_grid_data_path': ('brain_plotting', 'brain_grid_data_path'),
    'load_brain_grid_data': ('brain_plotting', 'load_brain_grid_data'),
    'plot_bregma_reference': ('brain_plotting', 'plot_bregma_reference'),
    'plot_bregma_reference_top': ('brain_plotting', 'plot_bregma_reference_top'),
    'plot_brain_grid': ('brain_plotting', 'plot_brain_grid'),
    'plot_brain_grid_top': ('brain_plotting', 'plot_brain_grid_top'),
    'plot_probe_track': ('brain_plotting', 'plot_probe_track'),
    'setup_brain_3d_axis': ('brain_plotting', 'setup_brain_3d_axis'),
    'setup_brain_top_axis': ('brain_plotting', 'setup_brain_top_axis'),
    'area_hex_colors': ('figure_brainmaps', 'area_hex_colors'),
    'build_brainmap_colormap': ('figure_brainmaps', 'build_brainmap_colormap'),
    'compute_diff_mean_for_trial_type': ('figure_brainmaps', 'compute_diff_mean_for_trial_type'),
    'draw_brainmap_reference': ('figure_brainmaps', 'draw_brainmap_reference'),
    'load_area_top_coordinates': ('figure_brainmaps', 'load_area_top_coordinates'),
    'load_opto_entries': ('figure_brainmaps', 'load_opto_entries'),
    'map_value_to_color': ('figure_brainmaps', 'map_value_to_color'),
    'align_decoding_matrices_by_session': ('figure_decoding', 'align_decoding_matrices_by_session'),
    'extract_decoding_signal_matrix': ('figure_decoding', 'extract_decoding_signal_matrix'),
    'load_decoding_npz': ('figure_decoding', 'load_decoding_npz'),
    'align_to_len': ('figure_data', 'align_to_len'),
    'figure_as_1d': ('figure_data', 'as_1d'),
    'figure_as_bool': ('figure_data', 'as_bool'),
    'clean_str': ('figure_data', 'clean_str'),
    'figure_completion_mask': ('figure_data', 'completion_mask'),
    'fiber_matches': ('figure_data', 'fiber_matches'),
    'hex_to_rgb01': ('figure_data', 'hex_to_rgb01'),
    'mean_sem_over_axis': ('figure_data', 'mean_sem_over_axis'),
    'nanmean_no_warn': ('figure_data', 'nanmean_no_warn'),
    'nanstd_no_warn': ('figure_data', 'nanstd_no_warn'),
    'keep_nonzero_paired_columns': ('figure_decoding', 'keep_nonzero_paired_columns'),
    'nonempty_indices': ('figure_decoding', 'nonempty_indices'),
    'normalize_entries': ('figure_data', 'normalize_entries'),
    'p_to_stars': ('figure_data', 'p_to_stars'),
    'to_dict_like': ('figure_data', 'to_dict_like'),
    'apply_fdr_correction': ('figure_opto_sessions', 'apply_fdr_correction'),
    'build_pvalue_table': ('figure_opto_sessions', 'build_pvalue_table'),
    'compute_area_window_session_metrics': ('figure_opto_sessions', 'compute_area_window_session_metrics'),
    'plot_paired_session_summary': ('figure_opto_sessions', 'plot_paired_session_summary'),
    'build_area_condition_psth_data': ('figure_psth', 'build_area_condition_psth_data'),
    'build_celltype_area_condition_psth_data': ('figure_psth', 'build_celltype_area_condition_psth_data'),
    'collect_area_condition_signal': ('figure_psth', 'collect_area_condition_signal'),
    'mean_sem_over_cells': ('figure_psth', 'mean_sem_over_cells'),
    'collect_area_roc_classification': ('figure_roc', 'collect_area_roc_classification'),
    'collect_area_roc_psth_data': ('figure_roc', 'collect_area_roc_psth_data'),
    'load_modulation_npz': ('figure_roc', 'load_modulation_npz'),
    'load_roc_npz': ('figure_roc', 'load_roc_npz'),
    'summarize_roc_classes': ('figure_roc', 'summarize_roc_classes'),
    'unwrap_roc_entry': ('figure_roc', 'unwrap_roc_entry'),
    'add_prettify_like_pvalues': ('figure_stats', 'add_prettify_like_pvalues'),
    'add_significance_simple': ('figure_stats', 'add_significance_simple'),
    'add_significance_stacked': ('figure_stats', 'add_significance_stacked'),
    'bh_fdr': ('figure_stats', 'bh_fdr'),
    'pairwise_group_pvals': ('figure_stats', 'pairwise_group_pvals'),
    'pairwise_kruskal_lsd_pvals': ('figure_stats', 'pairwise_kruskal_lsd_pvals'),
    'signrank_p_value': ('figure_stats', 'signrank_p_value'),
    'signrank_p_value_from_diff': ('figure_stats', 'signrank_p_value_from_diff'),
    'add_boxed_marker_legend': ('figure_style', 'add_boxed_marker_legend'),
    'add_colored_text_legend': ('figure_style', 'add_colored_text_legend'),
    'add_corner_scale_bar': ('figure_style', 'add_corner_scale_bar'),
    'add_line_legend': ('figure_style', 'add_line_legend'),
    'add_time_event_lines': ('figure_style', 'add_time_event_lines'),
    'style_bottom_time_axis': ('figure_style', 'style_bottom_time_axis'),
    'style_left_value_axis': ('figure_style', 'style_left_value_axis'),
    'style_plot_axis': ('figure_style', 'style_plot_axis'),
    'compute_mean_corr': ('figure_temporalcorr', 'compute_mean_corr'),
    'compute_temporal_corr_map_and_auc': ('figure_temporalcorr', 'compute_temporal_corr_map_and_auc'),
    'load_temporal_corr_npz': ('figure_temporalcorr', 'load_temporal_corr_npz'),
    'update_condition_matrix': ('figure_temporalcorr', 'update_condition_matrix'),
    'deep_scalar_obj': ('core', 'deep_scalar_obj'),
    'normalize_raw_entries': ('core', 'normalize_raw_entries'),
    'session_ids_for_area': ('figure_decoding', 'session_ids_for_area'),
    'to_1d': ('core', 'to_1d'),
    'to_1d_str': ('core', 'to_1d_str'),
    'to_session_id_str': ('core', 'to_session_id_str'),
    'unwrap_scalar_obj': ('core', 'unwrap_scalar_obj'),
    'window_index_bounds': ('figure_decoding', 'window_index_bounds'),
    '_entry_get_any': ('loading', '_entry_get_any'),
    '_entry_scalar_int': ('loading', '_entry_scalar_int'),
    '_infer_entry_n_trials': ('loading', '_infer_entry_n_trials'),
    '_infer_entry_n_units': ('loading', '_infer_entry_n_units'),
    '_normalize_vector_length': ('loading', '_normalize_vector_length'),
    'canonicalize_psth_entries_for_decoding': ('loading', 'canonicalize_psth_entries_for_decoding'),
    'decode_area_list': ('loading', 'decode_area_list'),
    'extract_fiber_location_from_trials': ('loading', 'extract_fiber_location_from_trials'),
    'get_nwb_trials_dataframe': ('loading', 'get_nwb_trials_dataframe'),
    'infer_placeholder_id_order': ('loading', 'infer_placeholder_id_order'),
    'load_canonical_movement_subspace_entries': ('loading', 'load_canonical_movement_subspace_entries'),
    'load_canonical_psth_entries_and_area_list': ('loading', 'load_canonical_psth_entries_and_area_list'),
    'load_canonical_spontlick_entries': ('loading', 'load_canonical_spontlick_entries'),
    'load_decoding_cluster_inputs': ('loading', 'load_decoding_cluster_inputs'),
    'load_nwb_behavior_timeseries_data': ('loading', 'load_nwb_behavior_timeseries_data'),
    'load_nwb_behavior_timeseries_timestamps': ('loading', 'load_nwb_behavior_timeseries_timestamps'),
    'load_psth_entries_and_area_list': ('loading', 'load_psth_entries_and_area_list'),
    'make_picklable': ('loading', 'make_picklable'),
    'matlab_like_trial_columns': ('loading', 'matlab_like_trial_columns'),
    'to_2d_xyz_rows': ('loading', 'to_2d_xyz_rows'),
    'to_numpy_compact_series': ('loading', 'to_numpy_compact_series'),
    'angle_btw_nd_vectors': ('math_utils', 'angle_btw_nd_vectors'),
    'auc_from_scores_labels': ('math_utils', 'auc_from_scores_labels'),
    'concat_rows': ('math_utils', 'concat_rows'),
    'corrcoef_with_pvalues': ('math_utils', 'corrcoef_with_pvalues'),
    'downsample_balance': ('math_utils', 'downsample_balance'),
    'ensure_trials_by_cells': ('math_utils', 'ensure_trials_by_cells'),
    'gram_schmidt_columns': ('math_utils', 'gram_schmidt_columns'),
    'holdout_split': ('math_utils', 'holdout_split'),
    'moving_sum_windows': ('math_utils', 'moving_sum_windows'),
    'nearest_bin': ('math_utils', 'nearest_bin'),
    'orthogonal_subspaces_pg': ('math_utils', 'orthogonal_subspaces_pg'),
    'pg_decompose_python': ('math_utils', 'pg_decompose_python'),
    'normalize_vec': ('math_utils', 'normalize_vec'),
    'project_trials': ('math_utils', 'project_trials'),
    'psth_simple_counts_single_event': ('math_utils', 'psth_simple_counts_single_event'),
    'qr_orth': ('math_utils', 'qr_orth'),
    'selectivity_index_calculation_np': ('math_utils', 'selectivity_index_calculation_np'),
    'zscore_matlab_cols': ('math_utils', 'zscore_matlab_cols'),
    'zscore_cols': ('math_utils', 'zscore_cols'),
    'bootstrap_notebook_env': ('paths', 'bootstrap_notebook_env'),
    'build_default_decoding_paths': ('paths', 'build_default_decoding_paths'),
    'build_default_ephys_psth_paths': ('paths', 'build_default_ephys_psth_paths'),
    'build_default_neuronal_data_paths': ('paths', 'build_default_neuronal_data_paths'),
    'build_default_optoinhibition_paths': ('paths', 'build_default_optoinhibition_paths'),
    'build_default_psth_area_output_paths': ('paths', 'build_default_psth_area_output_paths'),
    'ensure_repo_on_syspath': ('paths', 'ensure_repo_on_syspath'),
    'first_existing_path': ('paths', 'first_existing_path'),
    'resolve_repo_root': ('paths', 'resolve_repo_root'),
    'PsthBuildConfig': ('psth_build', 'PsthBuildConfig'),
    'assign_global_cluster_ids': ('psth_build', 'assign_global_cluster_ids'),
    'build_ephys_psth_session_from_nwb_path': ('psth_build', 'build_ephys_psth_session_from_nwb_path'),
    'build_ephys_psth_sessions': ('psth_build', 'build_ephys_psth_sessions'),
    'flatten_psth_sessions': ('psth_build', 'flatten_psth_sessions'),
    'save_psth_entries_npz': ('psth_build', 'save_psth_entries_npz'),
    'decode_one_bin_svm': ('svm', 'decode_one_bin_svm'),
    'decode_one_bin_svm_matlab_compat': ('svm', 'decode_one_bin_svm_matlab_compat'),
    'get_ccf_mask': ('selection', 'get_ccf_mask'),
    'get_celltype_mask': ('selection', 'get_celltype_mask'),
    'get_completion_mask': ('selection', 'get_completion_mask'),
    'get_quiet_mask': ('selection', 'get_quiet_mask'),
    'run_afterwhisker_decoding_pipeline': ('decoding_pipelines', 'run_afterwhisker_decoding_pipeline'),
    'run_cluster_dropout_decoding': ('dropout', 'run_cluster_dropout_decoding'),
    'run_coding_direction_pipeline': ('coding_direction', 'run_coding_direction_pipeline'),
    'run_delay_decoding_pipeline': ('decoding_pipelines', 'run_delay_decoding_pipeline'),
    'run_movement_subspace_decoding_pipeline': ('movement', 'run_movement_subspace_decoding_pipeline'),
    'run_nb_neurons_decoding_pipeline': ('decoding_pipelines', 'run_nb_neurons_decoding_pipeline'),
    'run_prewhisk_training_decoding_pipeline': ('decoding_pipelines', 'run_prewhisk_training_decoding_pipeline'),
    'run_random_dropout_decoding': ('dropout', 'run_random_dropout_decoding'),
    'run_spontlick_coding_direction_pipeline': ('coding_direction', 'run_spontlick_coding_direction_pipeline'),
    'build_cd_context': ('session_aware', 'build_cd_context'),
    'compute_context_metrics': ('session_aware', 'compute_context_metrics'),
    'extract_late_delay_matrix': ('session_aware', 'extract_late_delay_matrix'),
    'extract_trial_vectors_for_area': ('session_aware', 'extract_trial_vectors_for_area'),
    'extract_window_matrix': ('session_aware', 'extract_window_matrix'),
    'get_area_units': ('session_aware', 'get_area_units'),
    'get_available_areas_for_session': ('session_aware', 'get_available_areas_for_session'),
    'get_completed_trials': ('session_aware', 'get_completed_trials'),
    'get_context_trial_masks': ('session_aware', 'get_context_trial_masks'),
    'get_go_hit_trials': ('session_aware', 'get_go_hit_trials'),
    'get_go_whisker_trials': ('session_aware', 'get_go_whisker_trials'),
    'get_nogo_cr_trials': ('session_aware', 'get_nogo_cr_trials'),
    'get_nogo_whisker_trials': ('session_aware', 'get_nogo_whisker_trials'),
    'get_quiet_trials': ('session_aware', 'get_quiet_trials'),
    'get_session_quiet_trials': ('session_aware', 'get_session_quiet_trials'),
    'list_available_sessions': ('session_aware', 'list_available_sessions'),
    'project_trials_on_cd': ('session_aware', 'project_trials_on_cd'),
    'safe_pearson': ('session_aware', 'safe_pearson'),
    'safe_spearman': ('session_aware', 'safe_spearman'),
    'sem': ('session_aware', 'sem'),
    'to_bool_array': ('session_aware', 'to_bool_array'),
}

__all__ = sorted(_SYMBOL_SOURCES)


def __getattr__(name: str) -> Any:
    if name not in _SYMBOL_SOURCES:
        raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
    module_name, attr_name = _SYMBOL_SOURCES[name]
    module = importlib.import_module(f".{module_name}", __package__)
    value = getattr(module, attr_name)
    globals()[name] = value
    return value


def __dir__() -> list[str]:
    return sorted(list(globals().keys()) + __all__)
