"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const core_1 = require("../react_monad/core");
class Paginate extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.state = { page_index: 0, get_page_cache: "loading", current_page: "loading", page_cache: "loading" };
    }
    componentWillReceiveProps(new_props) {
        new_props.debug_info && console.log("New props:", new_props.debug_info());
        this.setState(Object.assign({}, this.state, { get_page_cache: new_props.get_page(this.state.page_index, new_props.items_per_page).comp(new_props.context)(callback => page => this.setState(Object.assign({}, this.state, { current_page: page, page_cache: new_props.renderer(page.items).comp(new_props.context)(new_props.cont) }))) }));
    }
    componentWillMount() {
        this.props.debug_info && console.log("Component will mount:", this.props.debug_info());
        this.setState(Object.assign({}, this.state, { get_page_cache: this.props.get_page(this.state.page_index, this.props.items_per_page).comp(this.props.context)(callback => page => this.setState(Object.assign({}, this.state, { current_page: page, page_cache: this.props.renderer(page.items).comp(this.props.context)(this.props.cont) }))) }));
    }
    goto(page_index) {
        this.setState(Object.assign({}, this.state, { get_page_cache: this.props.get_page(page_index, this.props.items_per_page).comp(this.props.context)(callback => page => this.setState(Object.assign({}, this.state, { page_index: page_index, current_page: page, page_cache: this.props.renderer(page.items).comp(this.props.context)(this.props.cont) }))) }));
    }
    render() {
        this.props.debug_info && console.log("Render:", this.props.debug_info());
        return React.createElement("div", { className: "monadic-paginated-content" },
            this.state.get_page_cache != "loading" ? this.state.get_page_cache : [],
            this.state.current_page != "loading" && this.state.current_page.num_pages > 1 ?
                React.createElement("div", { className: "monadic-paginator" },
                    this.state.current_page.page_index > 0 && this.state.current_page.num_pages > 3 ?
                        React.createElement("a", { className: "page first-page", style: { margin: "5px" }, onClick: () => this.goto(0) }, 1) : [],
                    this.state.current_page.page_index > 2 ? "..." : [],
                    this.state.current_page.page_index > 0 ?
                        React.createElement("a", { className: "page prev-page", style: { margin: "5px" }, onClick: () => this.goto(this.state.current_page != "loading" && this.state.current_page.page_index - 1) }, '<') : [],
                    React.createElement("span", { className: "page current-page", style: { margin: "5px" } }, this.state.current_page.page_index + 1),
                    this.state.current_page.page_index < this.state.current_page.num_pages - 1 ?
                        React.createElement("a", { className: "page next-page", style: { margin: "5px" }, onClick: () => this.state.current_page != "loading" && this.goto(this.state.current_page.page_index + 1) }, '>') : [],
                    this.state.current_page.page_index < this.state.current_page.num_pages - 2 ? "..." : [],
                    this.state.current_page.page_index < this.state.current_page.num_pages - 1 && this.state.current_page.num_pages > 3 ?
                        React.createElement("a", { className: "page last-page", style: { margin: "5px" }, onClick: () => this.state.current_page != "loading" && this.goto(this.state.current_page.num_pages - 1) }, this.state.current_page.num_pages) : null)
                :
                    [],
            this.state.page_cache != "loading" ? this.state.page_cache : []);
    }
}
exports.paginate = function (items_per_page, get_page, key, dbg) {
    return renderer => core_1.make_C(context => cont => React.createElement(Paginate, { kind: "paginate", items_per_page: items_per_page,
        get_page: get_page, renderer: renderer,
        cont: cont, context: context, key: key, debug_info: dbg }));
};
//# sourceMappingURL=paginator.js.map