import React from "react";

const CTXString = React.createContext("lol");

type StateBinding<T> = [get:T, set:React.StateUpdater<T>];
const CTXState = React.createContext(null) as React.Context<StateBinding<number>|null>;
const Outer =(props:{children:Preact.VNode})=>
{
    const binding = React.useState(11);
    return <CTXState.Provider value={binding}>
        {props.children}
    </CTXState.Provider>
};
const Inner =()=>
{
    const [stateGet, stateSet] = React.useContext(CTXState) || ["default", ()=>{}];
    return <button onClick={e=>stateSet((old)=>old+1)}>count: {stateGet} :)</button>
};


type Store = {name:string, age:number}
const reducer =(inState:Store, inAction:number)=>
{
    return {...inState, age:inState.age+inAction};
}

export default ()=>
{
    
    const [Store, Dispatch] = React.useReducer(reducer, {name:"seth", age:24} as Store)
    return <CTXString.Provider value="intradestink">
        <div>
            <h1>Title!</h1>
            <h2>subtitle!</h2>
            <p>
                <button onClick={e=>Dispatch(1)}>{Store.name}|{Store.age}?</button>
            </p>
        </div>
        <Outer>
            <Inner/>
        </Outer>
        <Outer>
            <Inner/>
        </Outer>
    </CTXString.Provider>
}