"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = __importStar(require("react"));
const PropTypes = __importStar(require("prop-types"));
const utils_1 = require("../../utils");
const immutability_helper_1 = __importDefault(require("immutability-helper"));
const BaseInputTemplate_1 = __importDefault(require("../templates/BaseInputTemplate"));
const ReactContext_1 = __importDefault(require("../../ReactContext"));
class TextSelectWidget extends React.Component {
    constructor(props) {
        super(props);
        this.onCheckBoxChange = (label) => () => {
            const selectedCheckboxes = this.state.selectedCheckboxes;
            if (selectedCheckboxes.has(label)) {
                selectedCheckboxes.delete(label);
            }
            else {
                selectedCheckboxes.add(label);
            }
            this.setState({ selectedCheckboxes: selectedCheckboxes });
            this.onChange(this.state.otherValue);
        };
        this.onInputChange = (value) => {
            this.onChange(value);
        };
        this.onChange = (textValue) => {
            let newValue = [...Array.from(this.state.selectedCheckboxes)];
            if (this.state.selectedCheckboxes.has(this.state.freeTextField)) {
                if (textValue && textValue.length > 0) {
                    newValue = (0, immutability_helper_1.default)(newValue, { $push: [textValue] });
                }
                newValue.splice(newValue.indexOf(this.state.freeTextField), 1);
            }
            this.props.onChange(newValue.join(", "));
        };
        this.state = this.getInitialState(props);
    }
    UNSAFE_componentWillReceiveProps(props) {
        this.setState(this.getStateFromProps(props));
    }
    getInitialState(props) {
        let { enums = [], delimiter = ", ", freeTextField } = (0, utils_1.getUiOptions)(props);
        const { value } = props;
        const selectedCheckboxes = new Set();
        const values = !value ? [] : value.split(delimiter);
        const otherValues = [];
        values.map(val => {
            if (enums.indexOf(val) !== -1) {
                selectedCheckboxes.add(val);
            }
            else {
                otherValues.push(val);
            }
        });
        if (otherValues.length > 0) {
            selectedCheckboxes.add(freeTextField);
        }
        return { selectedCheckboxes, enums, otherValue: otherValues.join(delimiter), freeTextField };
    }
    getStateFromProps(props) {
        let { enums = [], delimiter = ", " } = (0, utils_1.getUiOptions)(props);
        const { value } = props;
        const values = !value ? [] : value.split(delimiter);
        const otherValues = [];
        values.map(val => {
            if (enums.indexOf(val) === -1) {
                otherValues.push(val);
            }
        });
        return { otherValue: otherValues.join(delimiter) };
    }
    render() {
        let { enums, otherValue, freeTextField, selectedCheckboxes } = this.state;
        if (freeTextField) {
            enums = (0, immutability_helper_1.default)(enums, { $push: [freeTextField] });
        }
        const { Checkbox } = this.context.theme;
        return (React.createElement("div", { className: "laji-text-select" },
            enums.map((label, i) => {
                return (React.createElement(Checkbox, { key: i, title: label, checked: selectedCheckboxes.has(label), onChange: this.onCheckBoxChange(label) }, label));
            }),
            freeTextField ?
                React.createElement(BaseInputTemplate_1.default, Object.assign({}, this.props, { id: this.props.id + "_input", value: otherValue, onChange: this.onInputChange, disabled: !selectedCheckboxes.has(freeTextField) }))
                : null));
    }
}
TextSelectWidget.contextType = ReactContext_1.default;
TextSelectWidget.propTypes = {
    schema: PropTypes.shape({
        type: PropTypes.oneOf(["string"])
    }).isRequired,
    value: PropTypes.string
};
exports.default = TextSelectWidget;
