#include <behaviors.dtsi>
#include <dt-bindings/zmk/keys.h>
#include <dt-bindings/zmk/bt.h>

#define SY 1
#define OT 2

#define KEYL 0 1 2 3 \
             8 9 10 11 12 \
             18 19 20 21 \
             26 27 28 29 30 31
#define KEYR 4 5 6 7 \
             13 14 15 16 17 \
             22 23 24 25 \
             26 27 28 29 30 31

#define VOLDWN C_VOLUME_DOWN
#define VOLUP C_VOLUME_UP

#define TAPPING_TERM 280
#define QUICK_TAP 150
#define ALPHA_COMBO_REQUIRE_DELAY 100
#define COMBO_REQUIRE_DELAY 150

&caps_word {
    continue-list = <UNDERSCORE MINUS BACKSPACE DELETE LSHFT RSHFT>;
    mods = <(MOD_LSFT | MOD_RSFT)>;
    /delete-property/ ignore-modifiers;
};

&sk {
    quick-release;
    /delete-property/ ignore-modifiers;
};

/ {
    behaviors {
        hml: homerow_mods_left {
            compatible = "zmk,behavior-hold-tap";
            flavor = "balanced";
            tapping-term-ms = <TAPPING_TERM>;
            quick-tap-ms = <QUICK_TAP>;
            // maybe a bit lower than ALPHA_COMBO idk
            require-prior-idle-ms = <ALPHA_COMBO_REQUIRE_DELAY>;
            bindings = <&kp>, <&kp>;
            #binding-cells = <2>;
            hold-trigger-key-positions = <KEYR>;
            hold-trigger-on-release;
        };
        hmr: homerow_mods_right {
            compatible = "zmk,behavior-hold-tap";
            flavor = "balanced";
            tapping-term-ms = <TAPPING_TERM>;
            quick-tap-ms = <QUICK_TAP>;
            require-prior-idle-ms = <ALPHA_COMBO_REQUIRE_DELAY>;
            bindings = <&kp>, <&kp>;
            #binding-cells = <2>;
            hold-trigger-key-positions = <KEYL>;
            hold-trigger-on-release;
        };

        left_tab: left_shift_morph {
            compatible = "zmk,behavior-mod-morph";
            #binding-cells = <0>;
            bindings = <&kp TAB>, <&kp LEFT>;
            mods = <(MOD_LSFT|MOD_RSFT)>;
            keep-mods = <(MOD_LSFT)>;
        };
        right_space: right_space_morph {
            compatible = "zmk,behavior-mod-morph";
            #binding-cells = <0>;
            bindings = <&kp SPACE>, <&kp RIGHT>;
            mods = <(MOD_LSFT|MOD_RSFT)>;
            keep-mods = <(MOD_LSFT)>;
        };
        down_bspc: down_dspc_morph {
            compatible = "zmk,behavior-mod-morph";
            #binding-cells = <0>;
            bindings = <&kp BSPC>, <&kp DOWN>;
            mods = <(MOD_LSFT|MOD_RSFT)>;
            keep-mods = <(MOD_RSFT)>;
        };
        up_del: up_del_morph {
            compatible = "zmk,behavior-mod-morph";
            #binding-cells = <0>;
            bindings = <&kp DEL>, <&kp UP>;
            mods = <(MOD_LSFT|MOD_RSFT)>;
            keep-mods = <(MOD_RSFT)>;
        };

        lt_spc: layer_tap_spc {
            compatible = "zmk,behavior-hold-tap";
            flavor = "balanced";
            #binding-cells = <2>;
            bindings = <&mo>, <&right_space>;
            tapping-term-ms = <TAPPING_TERM>;
            quick-tap-ms = <QUICK_TAP>;
            hold-trigger-key-positions = <KEYR>;
            hold-trigger-on-release;
        };
        lt_bspc: layer_tap_bspc {
            compatible = "zmk,behavior-hold-tap";
            flavor = "balanced";
            #binding-cells = <2>;
            bindings = <&mo>, <&down_bspc>;
            tapping-term-ms = <TAPPING_TERM>;
            quick-tap-ms = <QUICK_TAP>;
            hold-trigger-key-positions = <KEYL>;
            hold-trigger-on-release;
        };

        ltsk: layer_tap_cap {
            compatible = "zmk,behavior-hold-tap";
            flavor = "balanced";
            #binding-cells = <2>;
            bindings = <&mo>, <&sk>;
            tapping-term-ms = <TAPPING_TERM>;
            quick-tap-ms = <QUICK_TAP>;
            hold-trigger-key-positions = <KEYR>;
            hold-trigger-on-release;
        };

        caps_lshift: lmod_morph_hold_tap {
            compatible = "zmk,behavior-mod-morph";
            #binding-cells = <0>;
            bindings = <&ltsk OT LSHIFT>, <&caps_word>;
            mods = <MOD_RSFT>;
        };

        caps_rshift: rmod_morph_hold_tap {
            compatible = "zmk,behavior-mod-morph";
            #binding-cells = <0>;
            bindings = <&ltsk OT RSHIFT>, <&caps_word>;
            mods = <MOD_LSFT>;
        };

        sm_reset: tap_dance_rst {
            compatible = "zmk,behavior-tap-dance";
            #binding-cells = <0>;
            tapping-term-ms = <TAPPING_TERM>;
            bindings = <&sys_reset>, <&bootloader>;
        };
    };

    keymap {
        compatible = "zmk,keymap";

        default_layer {
            bindings = <
//              ╭─────────────┬─────────────┬─────────────┬──────────────╮  ╭───────────────┬───────────────┬─────────────┬─────────────╮
                  &kp COMMA     &kp DOT       &kp P         &kp Y             &kp F           &kp G           &kp C         &kp R
//╭─────────────┼─────────────┼─────────────┼─────────────┼──────────────┤  ├───────────────┼───────────────┼─────────────┼─────────────┼─────────────╮
    &hml LALT A   &hml LGUI O   &hml LSFT E   &hml LCTL U   &kp I             &kp D           &hmr RCTL H     &hmr RSFT T   &hmr RGUI N   &hmr RALT S
//├─────────────┼─────────────┼─────────────┼─────────────┼──────────────╯  ╰───────────────┼───────────────┼─────────────┼─────────────┼─────────────┤
    &kp SQT       &kp Q         &kp J         &kp K                                           &kp M           &kp W         &kp V         &kp L
//╰───────────────────────────┼─────────────┼─────────────┼──────────────╮  ╭───────────────┼───────────────┼─────────────┼───────────────────────────╯
                                &left_tab     &lt_spc SY 0  &caps_lshift      &caps_rshift    &lt_bspc SY 0   &up_del
//                            ╰─────────────┴─────────────┴──────────────╯  ╰───────────────┴───────────────┴─────────────╯
            >;
        };

        symbols {
            bindings = <
//                  ╭───────────────┬────────────────┬────────────────┬─────────────╮  ╭─────────────┬─────────────┬─────────────┬─────────────╮
                      &kp EXCL        &kp AT           &kp HASH         &kp BSLH         &kp QMARK     &kp N7        &kp N8        &kp N9
//╭─────────────────┼───────────────┼────────────────┼────────────────┼─────────────┤  ├─────────────┼─────────────┼─────────────┼─────────────┼────────────────╮
    &hml LALT COLON   &hml LGUI DLLR  &hml LSFT PRCNT  &hml LCTL CARET  &kp FSLH         &kp EQUAL     &hmr RCTL N4  &hmr RSFT N5  &hmr RGUI N6  &hmr RALT PLUS
//├─────────────────┼───────────────┼────────────────┼────────────────┼─────────────╯  ╰─────────────┼─────────────┼─────────────┼─────────────┼────────────────┤
    &kp GRAVE         &kp AMPS        &kp STAR         &kp PIPE                                        &kp N1        &kp N2        &kp N3        &kp TILDE
//╰─────────────────────────────────┼────────────────┼────────────────┼─────────────╮  ╭─────────────┼─────────────┼─────────────┼──────────────────────────────╯
                                      &trans           &trans           &trans           &trans        &kp N0        &kp N0
//                                  ╰────────────────┴────────────────┴─────────────╯  ╰─────────────┴─────────────┴─────────────╯
            >;
        };

        others {
            bindings = <
//                  ╭───────────────┬────────────────┬────────────────┬─────────────╮  ╭─────────────┬─────────────┬─────────────┬─────────────╮
                      &none           &none            &none            &sm_reset        &sm_reset     &kp C_MUTE    &kp VOLDWN    &kp VOLUP
//╭─────────────────┼───────────────┼────────────────┼────────────────┼─────────────┤  ├─────────────┼─────────────┼─────────────┼─────────────┼────────────────╮
    &none             &none           &none            &none            &none            &bt BT_SEL 4  &bt BT_SEL 0  &bt BT_SEL 1  &bt BT_SEL 2  &bt BT_SEL 3
//├─────────────────┼───────────────┼────────────────┼────────────────┼─────────────╯  ╰─────────────┼─────────────┼─────────────┼─────────────┼────────────────┤
    &none             &none           &none            &none                                           &none         &none         &none         &bt BT_CLR
//╰─────────────────────────────────┼────────────────┼────────────────┼─────────────╮  ╭─────────────┼─────────────┼─────────────┼──────────────────────────────╯
                                      &trans           &trans           &trans           &kp C_PREV    &kp C_PP      &kp C_NEXT
//                                  ╰────────────────┴────────────────┴─────────────╯  ╰─────────────┴─────────────┴─────────────╯
            >;
        };
    };

    combos {
        compatible = "zmk,combos";
        combo_semi {
            // pressing both , and . does ;
            key-positions = <0 1>;
            bindings = <&kp SEMI>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_x {
            // pressing both . and p does x
            key-positions = <1 2>;
            bindings = <&kp X>;
            timeout-ms = <20>;
            require-prior-idle-ms = <ALPHA_COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_b {
            // pressing both c and r does b
            key-positions = <6 7>;
            bindings = <&kp B>;
            timeout-ms = <20>;
            require-prior-idle-ms = <ALPHA_COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_z {
            // pressing both g and c does z
            key-positions = <5 6>;
            bindings = <&kp Z>;
            timeout-ms = <20>;
            require-prior-idle-ms = <ALPHA_COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };

        combo_home {
            // pressing both oeu does home
            key-positions = <9 10 11>;
            bindings = <&kp HOME>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_end {
            // pressing both htn does end
            key-positions = <14 15 16>;
            bindings = <&kp END>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };

        combo_lbkt {
            // pressing .e does [
            key-positions = <1 10>;
            bindings = <&kp LBKT>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_rbkt {
            // pressing ct does ]
            key-positions = <6 15>;
            bindings = <&kp RBKT>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_lpar {
            // pressing pu does (
            key-positions = <2 11>;
            bindings = <&kp LPAR>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_rpar {
            // pressing gh does )
            key-positions = <5 14>;
            bindings = <&kp RPAR>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_lbrc {
            // pressing yi does {
            key-positions = <3 12>;
            bindings = <&kp LBRC>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_rbrc {
            // pressing fd does }
            key-positions = <4 13>;
            bindings = <&kp RBRC>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };

        combo_dash {
            // pressing jk does -
            key-positions = <20 21>;
            bindings = <&kp MINUS>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_under {
            // pressing mw does _
            key-positions = <22 23>;
            bindings = <&kp UNDERSCORE>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };

        combo_esc {
            // pressing e & SPC does esc
            key-positions = <10 27>;
            bindings = <&kp ESC>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
        combo_enter {
            // pressing e & BSP does enter
            key-positions = <15 30>;
            bindings = <&kp ENTER>;
            timeout-ms = <20>;
            require-prior-idle-ms = <COMBO_REQUIRE_DELAY>;
            layers = <0>;
        };
    };
};

// vim: ft=dts
