"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = img;
var _react = _interopRequireDefault(require("react"));
var _path = require("path");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
 * Copyright 2020 The Kubernetes Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* eslint-disable react/prop-types */

function allContentIsRemote(props) {
  return typeof props.baseUrl === 'string';
}
function handleImage(mdprops, props, key) {
  let {
    src
  } = props;
  const baseUrlIsHttp = mdprops.baseUrl && /http/i.test(mdprops.baseUrl);
  const srcIsHttp = /^http/i.test(src);
  const isHttp = srcIsHttp || baseUrlIsHttp;
  const isLocal = !isHttp && !allContentIsRemote(mdprops);
  const isClientRelativeMatch = !isHttp && src.match(/^@kui-shell\/client\/(images|icons)\/(.+)\.svg$/);
  if (isClientRelativeMatch) {
    const subdir = isClientRelativeMatch[1];
    if (subdir === 'icons') {
      src = require('@kui-shell/client/icons/' + isClientRelativeMatch[2] + '.svg');
    } else {
      src = require('@kui-shell/client/images/' + isClientRelativeMatch[2] + '.svg');
    }
  } else if (isLocal) {
    const absoluteSrc = (0, _path.isAbsolute)(src) ? src : (0, _path.join)(mdprops.fullpath ? (0, _path.dirname)(mdprops.fullpath) : mdprops.baseUrl || process.cwd(), src);
    src = absoluteSrc;
  } else if (!srcIsHttp && mdprops.baseUrl) {
    src = mdprops.baseUrl.replace(/{filename}/g, src);
  }
  const style = props && props.align ? {
    float: props.align
  } : undefined;
  return _react.default.createElement("img", {
    key: key,
    src: src,
    height: props.height,
    width: props.width,
    style: style,
    "data-float": props.align
  });
}
function img(mdprops) {
  return function Img(props) {
    return handleImage(mdprops, props) || _react.default.createElement("img", Object.assign({}, props));
  };
}