import "choerodon-ui/pro/lib/data-set/style";
import _DataSet from "choerodon-ui/pro/lib/data-set";

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, { createContext, useContext, useEffect, useMemo } from 'react';
import { inject } from 'mobx-react';
import { injectIntl } from 'react-intl';
import { withRouter } from 'react-router-dom';
import map from 'lodash/map';
import ListDataSet from "./ListDataSet";
import DetailDataSet from "./DetailDataSet";
import useStore from "./useStore";
import usePipelineStore from "./usePipelineStore";
import OptionsDataSet from "./OptionsDataSet";
var Store = /*#__PURE__*/createContext();
var STATUS = ['success', 'failed', 'operating'];
export function useDeployStore() {
  return useContext(Store);
}
export var StoreProvider = withRouter(injectIntl(inject('AppState')(function (props) {
  var projectId = props.AppState.currentMenuType.projectId,
      formatMessage = props.intl.formatMessage,
      children = props.children,
      state = props.location.state;
  var intlPrefix = 'c7ncd.deploy';
  var deployTypeDs = useMemo(function () {
    return new _DataSet({
      data: [{
        text: formatMessage({
          id: "".concat(intlPrefix, ".auto")
        }),
        value: 'auto'
      }, {
        text: formatMessage({
          id: "".concat(intlPrefix, ".manual")
        }),
        value: 'manual'
      }, {
        text: formatMessage({
          id: "".concat(intlPrefix, ".batch")
        }),
        value: 'batch'
      }],
      selection: 'single'
    });
  }, []);
  var deployModeDs = useMemo(function () {
    return new _DataSet({
      data: [{
        text: '容器部署',
        value: 'env'
      }, {
        text: '主机部署',
        value: 'host'
      }]
    });
  }, []);
  var deployResultDs = useMemo(function () {
    return new _DataSet({
      data: map(STATUS, function (item) {
        return {
          text: formatMessage({
            id: "".concat(intlPrefix, ".status.").concat(item)
          }),
          value: item
        };
      }),
      selection: 'single'
    });
  }, []);
  var deployStore = useStore();
  var pipelineStore = usePipelineStore();
  var envOptionsDs = useMemo(function () {
    return new _DataSet(OptionsDataSet());
  }, []);
  var pipelineOptionsDs = useMemo(function () {
    return new _DataSet(OptionsDataSet());
  }, []);
  var listDs = useMemo(function () {
    return new _DataSet(ListDataSet(intlPrefix, formatMessage, projectId, envOptionsDs, deployTypeDs, deployResultDs, pipelineOptionsDs, deployModeDs));
  }, [projectId]);
  var detailDs = useMemo(function () {
    return new _DataSet(DetailDataSet());
  }, []);
  useEffect(function () {
    if (state && state.pipelineId) {
      listDs.queryDataSet.getField('pipelineId').set('defaultValue', state.pipelineId);
    }
  }, []);
  useEffect(function () {
    envOptionsDs.transport.read.url = "/devops/v1/projects/".concat(projectId, "/envs/list_by_active?active=true"); // pipelineOptionsDs.transport.read.url =
    //   `/devops/v1/projects/${projectId}/pipeline/list_all`;

    envOptionsDs.query();
    pipelineOptionsDs.query();
  }, [projectId]);

  var value = _objectSpread(_objectSpread({}, props), {}, {
    prefixCls: 'c7ncd-deploy',
    permissions: ['devops-service.devops-deploy-record.pageByOptions', 'devops-service.app-service-instance.deploy', 'devops-service.pipeline.batchExecute', 'devops-service.pipeline.audit', 'devops-service.pipeline.retry', 'devops-service.pipeline.failed', 'devops-service.app-service-instance.batchDeployment'],
    intlPrefix: intlPrefix,
    listDs: listDs,
    detailDs: detailDs,
    deployStore: deployStore,
    pipelineStore: pipelineStore,
    envOptionsDs: envOptionsDs,
    pipelineOptionsDs: pipelineOptionsDs
  });

  return /*#__PURE__*/React.createElement(Store.Provider, {
    value: value
  }, children);
})));