"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const HelpBlock_1 = tslib_1.__importDefault(require("../../../components/HelpBlock"));
const Types_1 = require("../../../AdaptableState/Common/Types");
const rebass_1 = require("rebass");
const Radio_1 = tslib_1.__importDefault(require("../../../components/Radio"));
const FormLayout_1 = tslib_1.__importStar(require("../../../components/FormLayout"));
const ModuleConstants_1 = require("../../../Utilities/Constants/ModuleConstants");
const InfiniteTable_1 = require("../../../components/InfiniteTable");
const ALL_ENTITLEMENTS_MODULES = Types_1.ALL_MODULES;
const tableDOMProps = {
    style: {
        height: '100%',
        minWidth: '10rem',
        minHeight: 600,
    },
};
const columnTypes = {
    default: {
        defaultFlex: 1,
        align: 'center',
        defaultSortable: false,
    },
};
const headerOptions = {
    alwaysReserveSpaceForSortIcon: false,
};
const EntitlementsListForm = (props) => {
    let abOptions = props.adaptableOptions;
    const entitlements = abOptions?.entitlementOptions?.moduleEntitlements ?? [];
    if (typeof entitlements === 'function') {
        return (React.createElement(HelpBlock_1.default, { mt: 2 }, "Entitlements cannot be customized, they are handled by a custom function."));
    }
    const entitlementsMap = React.useMemo(() => {
        return entitlements.reduce((acc, entitlement) => {
            acc[entitlement.adaptableModule] = entitlement.accessLevel;
            return acc;
        }, {});
    }, [abOptions?.entitlementOptions]);
    const handleOnChange = React.useCallback((module, accessLevel) => {
        let newEntitlements = entitlements;
        // change
        if (entitlementsMap[module]) {
            newEntitlements = newEntitlements.map((entitlement) => {
                if (entitlement.adaptableModule === module) {
                    entitlement.accessLevel = accessLevel;
                }
                return entitlement;
            });
        }
        else {
            // new
            newEntitlements = [
                ...newEntitlements,
                { adaptableModule: module, accessLevel: accessLevel },
            ];
        }
        props.onChangedAptableOptions({
            ...abOptions,
            entitlementOptions: {
                ...abOptions.entitlementOptions,
                moduleEntitlements: newEntitlements,
            },
        });
    }, [abOptions?.entitlementOptions]);
    if (typeof entitlements === 'function') {
        return (React.createElement(HelpBlock_1.default, { mb: 2 }, "Entitlements cannot be customized, they are handled by a custom function."));
    }
    const columnsMap = {
        name: {
            header: 'Module Name',
            align: 'start',
            renderValue: (params) => {
                return React.createElement(React.Fragment, null, params.data.name);
            },
        },
        full: {
            header: 'Full',
            render: (params) => {
                const module = params.data.name;
                const accessLevel = entitlementsMap[module];
                return (React.createElement(Radio_1.default, { onClick: () => handleOnChange(module, 'Full'), checked: accessLevel === 'Full' }));
            },
        },
        readonly: {
            header: 'Read Only',
            render: (params) => {
                const module = params.data.name;
                const accessLevel = entitlementsMap[module];
                return (React.createElement(Radio_1.default, { onClick: () => handleOnChange(module, 'ReadOnly'), checked: accessLevel === 'ReadOnly' }));
            },
        },
        hidden: {
            header: 'Hidden',
            render: (params) => {
                const module = params.data.name;
                const accessLevel = entitlementsMap[module];
                return (React.createElement(Radio_1.default, { onClick: () => handleOnChange(module, 'Hidden'), checked: accessLevel === 'Hidden' }));
            },
        },
    };
    const data = ALL_ENTITLEMENTS_MODULES.map((module) => ({ name: ModuleConstants_1.ADAPTABLE_MODULE_MAP[module] }));
    let theme = 'light';
    if (typeof props.adaptableOptions.initialState === 'object' &&
        props.adaptableOptions.initialState.Theme?.CurrentTheme) {
        theme = props.adaptableOptions.initialState.Theme.CurrentTheme;
    }
    return (React.createElement(rebass_1.Box, { flex: 1, height: "100%", className: theme },
        React.createElement(InfiniteTable_1.DataSource, { data: data, primaryKey: "name" },
            React.createElement(InfiniteTable_1.InfiniteTableGrid, { columnTypes: columnTypes, headerOptions: headerOptions, domProps: tableDOMProps, columns: columnsMap }))));
};
const DefaultEntitlementForm = (props) => {
    const abOptions = props.adaptableOptions;
    const entitlementOptions = props.adaptableOptions?.entitlementOptions;
    const defaultEntitlement = entitlementOptions?.defaultAccessLevel;
    if (typeof defaultEntitlement === 'function') {
        return React.createElement(HelpBlock_1.default, { mb: 2 }, "Default Entitlement is controlled by a custom function");
    }
    const handleDefaultEntitlementChange = React.useCallback((accessLevel) => {
        props.onChangedAptableOptions({
            ...abOptions,
            entitlementOptions: {
                ...abOptions.entitlementOptions,
                defaultAccessLevel: accessLevel,
            },
        });
    }, [abOptions?.entitlementOptions?.defaultAccessLevel]);
    return (React.createElement(FormLayout_1.default, { mb: 2 },
        React.createElement(FormLayout_1.FormRow, { label: "Default Entitlement:" },
            React.createElement(Radio_1.default, { onClick: () => handleDefaultEntitlementChange('Full'), checked: defaultEntitlement === 'Full', mr: 3 }, "Visible"),
            React.createElement(Radio_1.default, { onClick: () => handleDefaultEntitlementChange('ReadOnly'), checked: defaultEntitlement === 'ReadOnly', mr: 3 }, "Read Only"),
            React.createElement(Radio_1.default, { onClick: () => handleDefaultEntitlementChange('Hidden'), checked: defaultEntitlement === 'Hidden' }, "Hidden"))));
};
const EntitlementsForm = (props) => {
    let abOptions = props.adaptableOptions;
    return (React.createElement(rebass_1.Flex, { height: "100%", flexDirection: "column", p: 2 },
        React.createElement(DefaultEntitlementForm, { ...props }),
        React.createElement(EntitlementsListForm, { ...props })));
};
exports.default = EntitlementsForm;
