"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = _interopRequireWildcard(require("react"));

var _Checkbox = _interopRequireDefault(require("@instructure/ui-forms/lib/components/Checkbox"));

var _View = _interopRequireDefault(require("@instructure/ui-layout/lib/components/View"));

var _TextInput = _interopRequireDefault(require("@instructure/ui-forms/lib/components/TextInput"));

var _formatMessage = _interopRequireDefault(require("../../format-message"));

var _ScreenReaderContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/ScreenReaderContent"));

/*
 * Copyright (C) 2018 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */
var AltTextForm =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(AltTextForm, _Component);

  function AltTextForm() {
    var _getPrototypeOf2;

    var _this;

    (0, _classCallCheck2.default)(this, AltTextForm);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf2.default)(AltTextForm)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.handleAltTextChange = function (e) {
      var state = (0, _objectSpread2.default)({}, _this.state, {
        altText: e.target.value
      });

      _this.setState(state);

      _this.props.altResolved(_this.isAltResolved(state));
    };

    _this.handleDecorativeCheckbox = function (e) {
      var state = (0, _objectSpread2.default)({}, _this.state, {
        decorativeSelected: e.target.checked
      });

      _this.setState(state);

      _this.props.altResolved(_this.isAltResolved(state));
    };

    _this.state = {
      altText: "",
      decorativeSelected: false
    };
    return _this;
  }

  (0, _createClass2.default)(AltTextForm, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.altTextField.focus();
    }
  }, {
    key: "isAltResolved",
    value: function isAltResolved(state) {
      return state.decorativeSelected || state.altText.length > 0;
    }
  }, {
    key: "value",
    value: function value() {
      return this.state;
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var alt_label = _react.default.createElement("span", null, _react.default.createElement("span", {
        "aria-hidden": true
      }, "Alternativ tekst: "), _react.default.createElement(_ScreenReaderContent.default, null, "Legg inn alternativ tekst som skildrar bildet"));

      var decorative_label = _react.default.createElement("span", null, _react.default.createElement("span", {
        "aria-hidden": true
      }, "Dekorativt bilde"), _react.default.createElement(_ScreenReaderContent.default, null, "Sjekk om bildet har ein dekorativ funksjon"));

      return _react.default.createElement("div", {
        className: "rcs-AltTextForm"
      }, _react.default.createElement(_TextInput.default, {
        ref: function ref(input) {
          _this2.altTextField = input;
        },
        label: alt_label,
        onChange: this.handleAltTextChange,
        name: "alt_text",
        disabled: this.state.decorativeSelected
      }), _react.default.createElement(_View.default, {
        margin: "x-small 0",
        display: "block"
      }, _react.default.createElement(_Checkbox.default, {
        label: decorative_label,
        name: "decorative",
        onChange: this.handleDecorativeCheckbox
      })));
    }
  }]);
  AltTextForm.displayName = "AltTextForm";
  return AltTextForm;
}(_react.Component);

exports.default = AltTextForm;
AltTextForm.propTypes = {
  altResolved: _propTypes.default.func.isRequired
};