import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/lib/tooltip";
import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/text-field/style";
import _TextField from "choerodon-ui/pro/lib/text-field";
import "choerodon-ui/pro/lib/icon/style";
import _Icon from "choerodon-ui/pro/lib/icon";
import "choerodon-ui/pro/lib/button/style";
import _Button from "choerodon-ui/pro/lib/button";
import "choerodon-ui/pro/lib/select/style";
import _Select from "choerodon-ui/pro/lib/select";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { useEffect, useState, Fragment } from 'react';
import { inject } from 'mobx-react';
import { observer } from 'mobx-react-lite';
import { injectIntl } from 'react-intl';
import { Header, Choerodon, HeaderButtons, ButtonGroup } from '@choerodon/boot';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import _ from 'lodash';
import handleMapStore from "../main-view/store/handleMapStore";
import { useCodeManagerStore } from "../stores";
import "./index.less";
var Option = _Select.Option,
    OptGroup = _Select.OptGroup;
var CodeManagerToolBar = injectIntl(inject('AppState')(observer(function (props) {
  var _useCodeManagerStore = useCodeManagerStore(),
      appServiceDs = _useCodeManagerStore.appServiceDs,
      selectAppDs = _useCodeManagerStore.selectAppDs;

  useEffect(function () {
    handleRefresh();
  }, [selectAppDs.current]);
  var name = props.name,
      formatMessage = props.intl.formatMessage,
      type = props.type;

  var currentApp = _.find(appServiceDs.toData(), ['id', selectAppDs.current.get('appServiceId')]);

  var noRepoUrl = formatMessage({
    id: 'repository.noUrl'
  });

  var getSelfToolBar = function getSelfToolBar() {
    var obj = handleMapStore[name] && handleMapStore[name].getSelfToolBar && handleMapStore[name].getSelfToolBar();
    return obj || null;
  };

  var getSelfToolBarObj = function getSelfToolBarObj() {
    var obj = handleMapStore[name] && handleMapStore[name].getSelfToolBarObj && handleMapStore[name].getSelfToolBarObj();
    return obj || null;
  };
  /**
   * 点击复制代码成功回调
   * @returns {*|string}
   */


  var handleCopy = function handleCopy() {
    Choerodon.prompt('复制成功');
  };

  var handleRefresh = function handleRefresh() {
    handleMapStore[name] && handleMapStore[name].refresh();
  };

  var refreshApp = function refreshApp() {
    appServiceDs.query().then(function (data) {
      if (data && data.length && data.length > 0) {
        selectAppDs.current.set('appServiceId', selectAppDs.current.get('appServiceId') || data[0].id);
        handleRefresh();
      }
    });
  };

  return type && type === 'button' ? [getSelfToolBar(), /*#__PURE__*/React.createElement(_Button, {
    onClick: refreshApp,
    icon: "refresh"
  }, formatMessage({
    id: 'refresh'
  }))] : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, null, /*#__PURE__*/React.createElement(HeaderButtons, {
    items: function () {
      var item = getSelfToolBarObj();
      var res = [{
        icon: 'refresh',
        display: true,
        handler: refreshApp,
        iconOnly: true
      }];

      if (item) {
        return [_objectSpread({}, getSelfToolBarObj()), {
          icon: 'refresh',
          display: true,
          handler: refreshApp,
          iconOnly: true
        }];
      }

      return res;
    }(),
    showClassName: false
  })));
})));
export default CodeManagerToolBar;
export var SelectApp = injectIntl(inject('AppState')(observer(function (props) {
  var codeManagerStore = useCodeManagerStore();
  var appServiceDs = codeManagerStore.appServiceDs,
      selectAppDs = codeManagerStore.selectAppDs,
      projectId = codeManagerStore.projectId;
  var formatMessage = props.intl.formatMessage,
      theme4 = props.theme4;

  var currentApp = _.find(appServiceDs.toData(), ['id', selectAppDs.current.get('appServiceId')]);

  var noRepoUrl = formatMessage({
    id: 'repository.noUrl'
  });

  var handleCopy = function handleCopy() {
    Choerodon.prompt('复制成功');
  };

  var copyMenu = /*#__PURE__*/React.createElement("div", {
    role: "none",
    className: "c7ncd-copyMenu c7n-pro-form-float",
    onClick: function onClick(e) {
      return e.stopPropagation();
    }
  }, /*#__PURE__*/React.createElement(_Form, null, /*#__PURE__*/React.createElement(_TextField, {
    disabled: true,
    defaultValue: currentApp && currentApp.sshRepositoryUrl || noRepoUrl,
    label: formatMessage({
      id: 'repository.SSHaddress'
    }),
    addonAfter: /*#__PURE__*/React.createElement(CopyToClipboard, {
      text: currentApp && currentApp.sshRepositoryUrl || noRepoUrl,
      onCopy: handleCopy
    }, /*#__PURE__*/React.createElement(_Icon, {
      type: "content_copy",
      style: {
        cursor: 'pointer'
      }
    }))
  }), /*#__PURE__*/React.createElement(_TextField, {
    disabled: true,
    defaultValue: currentApp && currentApp.repoUrl || noRepoUrl,
    label: formatMessage({
      id: 'repository.HTTPSaddress'
    }),
    addonAfter: /*#__PURE__*/React.createElement(CopyToClipboard, {
      text: currentApp && currentApp.repoUrl || noRepoUrl,
      onCopy: handleCopy
    }, /*#__PURE__*/React.createElement(_Icon, {
      type: "content_copy",
      style: {
        cursor: 'pointer'
      }
    }))
  })));

  function rendererAppServiceId(_ref) {
    var value = _ref.value,
        text = _ref.text;
    var record = appServiceDs.find(function (appServiceRecord) {
      return appServiceRecord.get('id') === value;
    });

    try {
      return "".concat(text, "(").concat(record.get('code'), ")");
    } catch (e) {
      return text;
    }
  }

  function renderAppServiceOption(_ref2) {
    var value = _ref2.value,
        text = _ref2.text;
    var record = appServiceDs.find(function (appServiceRecord) {
      return appServiceRecord.get('id') === value;
    });

    if (record) {
      return /*#__PURE__*/React.createElement(_Tooltip, {
        title: record.get('code')
      }, rendererAppServiceId({
        value: value,
        text: text
      }));
    }

    return '';
  }

  function renderSearchMatcher(_ref3) {
    var record = _ref3.record,
        text = _ref3.text;
    var tempRecord = appServiceDs.find(function (appServiceRecord) {
      return appServiceRecord.get('id') === record.get('value');
    });
    return tempRecord.get('code').indexOf(text) !== -1 || tempRecord.get('name').indexOf(text) !== -1;
  }

  return /*#__PURE__*/React.createElement("div", {
    style: {
      paddingLeft: 24,
      display: 'flex',
      alignItems: 'center'
    }
  }, /*#__PURE__*/React.createElement(_Form, Object.assign({}, theme4 ? {
    labelLayout: 'horizontal'
  } : {
    labelLayout: 'float'
  }, {
    columns: 2,
    style: {
      maxWidth: '5.5rem'
    }
  }), /*#__PURE__*/React.createElement(_Select, {
    addonBefore: theme4 ? '应用服务' : undefined,
    colSpan: 1,
    className: "c7ncd-cm-select",
    label: theme4 ? undefined : formatMessage({
      id: 'c7ncd.deployment.app-service'
    }),
    dataSet: selectAppDs,
    notFoundContent: appServiceDs.length === 0 ? formatMessage({
      id: 'ist.noApp'
    }) : '未找到应用服务',
    searchable: true,
    searchMatcher: renderSearchMatcher,
    name: "appServiceId",
    clearButton: false,
    disabled: appServiceDs.status !== 'ready' || appServiceDs.length === 0,
    optionRenderer: renderAppServiceOption,
    renderer: function renderer(_ref4) {
      var text = _ref4.text;
      return text;
    }
  }, localStorage.getItem('recent-app') && /*#__PURE__*/React.createElement(OptGroup, {
    label: formatMessage({
      id: 'deploy.app-recent'
    }),
    key: "app-recent"
  }, _.map(JSON.parse(localStorage.getItem('recent-app'))[projectId], function (_ref5, index) {
    var id = _ref5.id,
        code = _ref5.code,
        opName = _ref5.name;
    return /*#__PURE__*/React.createElement(Option, {
      value: id,
      key: index
    }, opName);
  })), /*#__PURE__*/React.createElement(OptGroup, {
    label: formatMessage({
      id: 'deploy.app'
    }),
    key: "app"
  }, _.map(appServiceDs.toData(), function (_ref6, index) {
    var id = _ref6.id,
        code = _ref6.code,
        opName = _ref6.name;
    return /*#__PURE__*/React.createElement(Option, {
      value: id,
      key: index
    }, opName);
  }))), /*#__PURE__*/React.createElement(ButtonGroup, {
    name: formatMessage({
      id: 'repository.copyUrl'
    }),
    renderCustomDropDownPanel: function renderCustomDropDownPanel(setvisib) {
      return copyMenu;
    }
  })));
})));