"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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
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 components_1 = require("../components");
const LajiForm_1 = __importDefault(require("../LajiForm"));
const MapField_1 = require("./MapField");
const ReactContext_1 = __importDefault(require("../../ReactContext"));
class LocalityField extends React.Component {
    constructor() {
        super(...arguments);
        this.state = {};
        this.showEditor = () => {
            this.setState({ modal: true });
        };
        this.hideEditor = () => {
            this.setState({ modal: false });
        };
        this.setFormRef = (elem) => {
            this.lajiForm = elem;
        };
        this.renderModal = () => {
            const _a = this.props, { onChange } = _a, props = __rest(_a, ["onChange"]);
            const { Modal } = this.context.theme;
            return (React.createElement(Modal, { dialogClassName: "laji-form image-modal", show: true, onHide: this.hideEditor },
                React.createElement(Modal.Header, { closeButton: true }),
                React.createElement(Modal.Body, null,
                    React.createElement(LajiForm_1.default, Object.assign({ ref: this.setFormRef }, props, { uiSchema: (0, utils_1.getInnerUiSchema)(this.props.uiSchema), onSubmit: this.onSubmit, lang: this.props.formContext.lang, showShortcutButton: false, renderSubmit: false, validators: {}, warnings: {}, apiClient: this.props.formContext.apiClient.apiClient, uiSchemaContext: this.props.formContext.uiSchemaContext })),
                    React.createElement(components_1.Button, { block: true, onClick: this.onSubmitClick }, "OK"),
                    React.createElement(components_1.Button, { block: true, onClick: this.hideEditor }, this.props.formContext.translations.Cancel))));
        };
        this.onSubmitClick = (e) => {
            this.lajiForm._onDefaultSubmit(e);
        };
        this.onSubmit = ({ formData }) => {
            this.props.onChange(formData);
            this.hideEditor();
        };
    }
    render() {
        const { radius: _radius, geometry } = (0, utils_1.getUiOptions)(this.props.uiSchema);
        const fields = Object.keys(this.props.schema.properties);
        const values = fields.filter(s => !(0, utils_1.isEmptyString)(this.props.formData[s])).map(f => this.props.formData[f]);
        const radius = typeof _radius === "number"
            ? _radius
            : geometry
                ? (0, MapField_1.getCenterAndRadiusFromGeometry)(geometry).radius
                : undefined;
        if (typeof radius === "number") {
            values.push(`(${this.props.formContext.translations.accuracy}: ${parseInt(radius)}m)`);
        }
        const { Panel, Row, Col } = this.context.theme;
        return (React.createElement(Row, null,
            React.createElement(Col, { xs: 12 },
                React.createElement(Panel, { className: (0, utils_1.getUiOptions)(this.props.uiSchema).panelClassName },
                    React.createElement(Panel.Body, null,
                        values.map((v, i) => (React.createElement("span", { key: i },
                            v,
                            i < values.length - 1 ? ", " : ""))),
                        React.createElement(components_1.GlyphButton, { onClick: this.showEditor, glyph: "pencil", variant: "default", className: "pull-right" })))),
            this.state.modal && this.renderModal()));
    }
}
LocalityField.contextType = ReactContext_1.default;
LocalityField.propTypes = {
    uiSchema: PropTypes.object,
    schema: PropTypes.shape({
        type: PropTypes.oneOf(["object"])
    }).isRequired,
    formData: PropTypes.object.isRequired
};
exports.default = LocalityField;
