define('@cardstack/tools/components/cs-version-control', ['exports', '@cardstack/tools/templates/components/cs-version-control', 'ember-concurrency', '@cardstack/tools/private-api', '@cardstack/rendering/helpers/cs-model-type', '@cardstack/plugin-utils/environment'], function (exports, _csVersionControl, _emberConcurrency, _privateApi, _csModelType, _environment) {
  'use strict';

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = Ember.Component.extend({
    layout: _csVersionControl.default,
    tagName: '',
    opened: true,
    animationRules: animationRules,
    resourceMetadata: Ember.inject.service(),
    store: Ember.inject.service(),

    modelMeta: Ember.computed('model', function () {
      return this.get('resourceMetadata').read(this.get('model'));
    }),

    modelType: Ember.computed('model', function () {
      return (0, _csModelType.modelType)(this.get('model'));
    }),

    onMaster: Ember.computed('modelMeta.branch', function () {
      var branch = this.get('modelMeta').branch;
      return branch == null || branch === _environment.defaultBranch;
    }),

    upstreamMeta: Ember.computed('upstreamModel', function () {
      var upstream = this.get('upstreamModel');
      if (upstream) {
        return this.get('resourceMetadata').read(upstream);
      }
    }),

    fetchUpstreamModel: (0, _emberConcurrency.task)( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var model, type, id, upstreamModel;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              this.set('upstreamModel', null);

              if (!this.get('onMaster')) {
                _context.next = 3;
                break;
              }

              return _context.abrupt('return');

            case 3:
              model = this.get('model');
              type = (0, _csModelType.modelType)(model);
              id = model.get('id');

              if (!(id == null)) {
                _context.next = 8;
                break;
              }

              return _context.abrupt('return');

            case 8:
              _context.prev = 8;
              _context.next = 11;
              return this.get('store').findRecord('cardstack-generic', 'master/' + type + '/' + id);

            case 11:
              upstreamModel = _context.sent;

              this.set('upstreamModel', upstreamModel);
              _context.next = 20;
              break;

            case 15:
              _context.prev = 15;
              _context.t0 = _context['catch'](8);

              if (!(_context.t0.isAdapterError && _context.t0.errors && _context.t0.errors.length > 0 && _context.t0.errors[0].code === 404)) {
                _context.next = 19;
                break;
              }

              return _context.abrupt('return');

            case 19:
              throw _context.t0;

            case 20:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, this, [[8, 15]]);
    })).observes('modelMeta.branch', 'model.id').on('init'),

    // this describes the state of our model relative to its branch. So
    // "saved" here means it has been saved to its branch, etc.
    modificationState: Ember.computed('model.isNew', 'anythingDirty', function () {
      if (this.get('model.isNew')) {
        return "new";
      } else if (this.get('anythingDirty')) {
        return "changed";
      } else {
        return "saved";
      }
    }),

    // this describes the state of our model relative to its value on
    // the default branch.
    upstreamState: Ember.computed('upstreamMeta.hash', 'modelMeta.hash', 'onMaster', 'fetchUpstreamModel.isRunning', function () {
      if (this.get('onMaster')) {
        return 'self';
      }

      if (this.get('fetchUpstreamModel.isRunning')) {
        return 'pending';
      }

      var upstreamMeta = this.get('upstreamMeta');
      if (!upstreamMeta) {
        return 'created';
      }

      var meta = this.get('modelMeta');
      if (meta.hash === upstreamMeta.hash) {
        return 'same';
      } else {
        return 'different';
      }
    }),

    titleClass: Ember.computed('upstreamState', function () {
      var state = this.get('upstreamState');
      if (state === 'created' || state === 'different') {
        return 'cs-version-control-preview';
      }
    }),

    title: Ember.computed('modificationState', 'upstreamState', function () {
      switch (this.get('modificationState')) {
        case 'new':
          return 'Drafted';
        case 'changed':
          return 'Changed';
        case 'saved':
          switch (this.get('upstreamState')) {
            case 'pending':
              return '...';
            case 'self':
              return 'Live';
            case 'created':
            case 'different':
            case 'same':
              return 'Preview';
          }
      }
    }),

    anythingDirty: Ember.computed('model.{hasDirtyFields,hasDirtyAttributes}', function () {
      // hasDirtyFields comes from the ember-data-relationship-tracker
      // addon, if it's available. It's fine if it's not since the value
      // will default to false, you just don't get relationship dirty
      // tracking
      return this.get('model.hasDirtyFields') || this.get('model.hasDirtyAttributes');
    }),

    anythingPending: Ember.computed('model.isNew', 'anythingDirty', function () {
      return this.get('model.isNew') || this.get('anythingDirty');
    }),

    update: (0, _emberConcurrency.task)( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return this.get('model').save();

            case 2:
            case 'end':
              return _context2.stop();
          }
        }
      }, _callee2, this);
    })).keepLatest(),

    delete: (0, _emberConcurrency.task)( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
      var model, placeholder, branch, route;
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              model = this.get('model');
              placeholder = { isCardstackPlaceholder: true, type: (0, _csModelType.modelType)(model), slug: model.get('slug') };

              if (!model.get('isNew')) {
                _context3.next = 5;
                break;
              }

              (0, _privateApi.transitionTo)(Ember.getOwner(this), 'cardstack.new-content', [placeholder]);
              return _context3.abrupt('return');

            case 5:
              branch = this.get('modelMeta.branch');

              this.get('resourceMetadata').write(placeholder, { branch: branch });
              route = this.get('cardstackRouting').routeFor((0, _csModelType.modelType)(model), model.get('slug'), branch);
              _context3.next = 10;
              return this.get('model').destroyRecord();

            case 10:
              if (route) {
                (0, _privateApi.transitionTo)(Ember.getOwner(this), route.name, [placeholder], route.queryParams);
              }

            case 11:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, this);
    })),

    actions: {
      open: function open() {
        this.set('opened', true);
      },
      close: function close() {
        this.set('opened', false);
      }
    }
  });


  function animationRules() {
    this.transition(this.fromValue(false), this.toValue(true), this.use('to-down', { duration: 250 }), this.reverse('to-up', { duration: 250 }));
  }
});